标签:multi 指令 xxxx 结束 data 概念 span target main
进程的概念:
python中进程的操作
process模块是一个创建进程的模块,借助这个模块,就可以完成进程的创建。
1)进程的使用
import multiprocessing import time def work1(): for i in range(10): print("正在运行work1.....", i, "子进程编号:", multiprocessing.current_process().pid) time.sleep(0.5) if __name__ == ‘__main__‘: process_obj = multiprocessing.Process(target=work1, name="p1") print("主进程编号:", multiprocessing.current_process().pid) process_obj.start()
2)进程参数的传递
import multiprocessing import time def work1(a, b, c): print("参数:", a, b, c) for i in range(10): print("正在运行work1....") time.sleep(0.5) if __name__ == ‘__main__‘: # 1.args传递元祖 # process_obj = multiprocessing.Process(target=work1, args=(1, 2, 3)) # 2.kwargs传递字典 # process_obj = multiprocessing.Process(target=work1, kwargs={"a": 1, "b": 2, "c": 3}) # 3.args与kwargs混合传递 process_obj = multiprocessing.Process(target=work1, args=(1,), kwargs={"c": 3, "b": 2}) process_obj.start()
3)进程间全局变量不能共享
import multiprocessing import time g_num = 0 def work1(): global g_num for i in range(10): g_num += 1 print("work1.....", g_num) # 10 def work2(): print("work2.....", g_num) # 0 if __name__ == ‘__main__‘: p1 = multiprocessing.Process(target=work1) p2 = multiprocessing.Process(target=work2) p1.start() p2.start() time.sleep(2) print("主进程....", g_num) # 0
4)进程的守护
import multiprocessing import time def work(): for i in range(10): print("子进程运行中....", i) time.sleep(0.5) if __name__ == ‘__main__‘: p = multiprocessing.Process(target=work) # 进程守护:子进程与主进程的一种约束,当主进程结束时,子进程也随之结束。 p.daemon = True p.start() time.sleep(2) exit() print("xxxxxx")
标签:multi 指令 xxxx 结束 data 概念 span target main
原文地址:https://www.cnblogs.com/wakey/p/13258870.html