创建一个多进程(multiprocessing.Process)

进程是资源的集合,每个进程至少包含一个线程 

import multiprocessing #导入进程模块
import time, threading #导入线程

def thread_run():
print(threading.get_ident())
def run(name):
time.sleep(2)
print('hello', name)
t = threading.Thread(target=thread_run) #创建一个线程
t.start()


if __name__ == '__main__':
'''
在进程中创建一个线程
'''

for i in range(10): #循环十次
p = multiprocessing.Process(target=run, args=(i, )) #在进程中添加线程
p.start()

猜你喜欢

转载自www.cnblogs.com/my-love-is-python/p/9164437.html