multiprocessing
为了解决GIL用不了多核的问题
# from multiprocessing import Process
#
# import time
#
# def run():
# time.sleep(2)
# print('hello')
# p_list = []
#
# if __name__ == '__main__':
# for i in range(5):
# p = Process(target=run)
# p_list.append(p)
# p.start()
# for p in p_list:
# p.join()
# print('end')
#
from multiprocessing import Process
import time
class MyProcess(Process):
# def __init__(self, name):
# super(MyProcess, self).__init__()
# self.name = name
def run(self):
time.sleep(2)
print('hello motor', self.name, time.ctime())
if __name__ == '__main__':
p_list = []
for i in range(5):
p = MyProcess()
p.daemon = True # 守护进程
p.start()
p_list.append(p)
# for p in p_list:
# p.join()
print('end...')
from multiprocessing import Process
import time, os
def info(title):
print('title:', title)
print('parent process:', os.getppid()) # 父进程号
print('process id:', os.getpid()) # 进程号
if __name__ == '__main__':
info('main process line')
time.sleep(1)
print('-------------------------')
p = Process(target=info, args=['lupe'])
p.start()