【Python】開啟子進程並獲取PID


方式一
# import time
# from multiprocessing import Process
#
#
# def task(name):
# print('%s is running ' % name)
# time.sleep(3)
# print('%s is done' % name)
#
#
# if __name__ == '__main__':
# p = Process(target=task, kwargs={'name': '子進程1'}) # 調用task 傳入參數name
# p.start() # 僅僅給操作系統發送信號
#
# print('主')

方式二
import time,os
from multiprocessing import Process


class MyProcess(Process):
def __init__(self, name):
super(MyProcess, self).__init__()
self.name = name

def run(self): # 固定名字run, 會被start調用
print('child <%s> is running, parent is <%s>' % (os.getpid(), os.getppid()))
time.sleep(3)
print('child <%s> is done, parent is <%s>' % (os.getpid(), os.getppid()))


if __name__ == '__main__':
p = MyProcess('子進程')
p.start()
print('主進程<%s>, pycharm<%s>' % (os.getpid(), os.getppid()))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM