Python--多进程--01

multiprocess

import multiprocessing
import time
def worker_1(interval):
    print(' i am worker1')
	n=5
	while n>0:
		print(time.ctime())
		n-=1
		time.sleep(interval)

def worker_2(interval):
	print(' i am worker2')
    print(interval)
def worker_3(interval):
	print(' i am worker3')
    print()


if __name__=="__main__":
	p1=multiprocessing.Process(target=worker_1,args=(1,))
	p2=multiprocessing.Process(target=worker_2,args=(3,))
	p3=multiprocessing.Process(target=worker_3,args=(5,))
	p1.start()
	p2.start()
	p3.start()
    

stdout

i am worker1
Sun Jan  7 13:46:50 2018
 i am worker2
 i am worker3
Sun Jan  7 13:46:51 2018
Sun Jan  7 13:46:52 2018
Sun Jan  7 13:46:53 2018
Sun Jan  7 13:46:54 2018

AttributeError:module 'main' has no attribute 'spec'

  • 在ipython解释器中运行以上脚本一直弹出这个问题,AttributeError
    查阅官方文档,In the remaining cases __main__.__spec__ is set to None, as the code used to populate
    the __main__ does not correspond directly with an importable module
    • interactive prompt
    • -c switch
    • running from stdin
    • running directly from a source or bytecode fil
  • 猜想应该是IPython解释器运行时,__main__是一个特殊的import system,在IPython无法将多进程初始化为None值,事实证明,在pycharm中运行不存在这个问题
posted @ 2018-01-07 14:09  天波-风客  阅读(318)  评论(0编辑  收藏  举报