欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > [数据采集技术:实践03]:Process 类(方法,属性),os 和 time 模块的使用

[数据采集技术:实践03]:Process 类(方法,属性),os 和 time 模块的使用

2025/3/10 12:02:15 来源:https://blog.csdn.net/Abraxs/article/details/142907267  浏览:    关键词:[数据采集技术:实践03]:Process 类(方法,属性),os 和 time 模块的使用

数据采集技术实验三(多进程)

一:要求

1.创建项目文件夹,命名为:学号-3,如:20220001-3。后续所有文
件均放在此文件夹中。

二:Process 类的方法和属性及os 模块和 time 模块的使用

2.新建 test1.py 文件,使用 Process 类的方法和属性,创建两个子
进程,分别使用 os 模块和 time 模块,输出父进程和子进程的 ID 及
子进程的时间。

2.1:代码实现

import os
import time
import multiprocessing# 定义第一个进程01的ID和当前时间
def child_process_1():print(f"子进程-01 的ID: {os.getpid()}")print(f"子进程-01 的时间: {time.ctime()}")# 定义第二个进程02的ID和当前时间
def child_process_2():print(f"子进程02的ID: {os.getpid()}")start_time = time.time()time.sleep(2)elasped_time = time.time() - start_timeprint(f"子进程 2 的经过时间: {elasped_time} 秒")print(f"子进程 2 的时间: {time.ctime()}")if __name__ == '__main__':# 获取父进程IDparent_id = os.getpid()print(f"父进程的ID: {parent_id}")# 创建第一个子进程process_1 = multiprocessing.Process(target=child_process_1)# 创建第二个子进程process_2 = multiprocessing.Process(target=child_process_2)# 启动子进程process_1.start()process_2.start()# 等待子进程完成process_1.join()process_2.join()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词