self_example/Spider/chapter01_线程相关/多进程/MyMutiProcess.py

41 lines
991 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#-*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/10/17 20:59
@Usage : 学习多进程库mutiProcessing的使用
@Desc :
'''
'''
参考:
[1] https://cuiqingcai.com/3335.html
[2] https://blog.csdn.net/qq_38120851/article/details/122504028
'''
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self, loop):
Process.__init__(self)
self.loop = loop
def run(self):
for count in range(self.loop):
time.sleep(1)
print('Pid: ' + str(self.pid) + ' LoopCount: ' + str(count))
if __name__ == '__main__':
# 在这里介绍一个属性,叫做 deamon。每个线程都可以单独设置它的属性如果设置为 True当父进程结束后子进程会自动被终止。
# 如果这里不join只会打印Main process Ended!
for i in range(2, 10):
p = MyProcess(i)
p.daemon = True
p.start()
p.join()
print("Main process Ended!")