zoukankan      html  css  js  c++  java
  • 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中运行不存在这个问题
  • 相关阅读:
    Python_Excel文件操作
    Python_CRC32
    Python_替换当前目录下文件类型
    Python_os、os.path、os.shutil使用案例
    Python_文件与文件夹操作
    MyBatis/Ibatis中#和$的区别
    遍历listmap 遍历map
    jquery操作select(取值,设置选中)
    ==与===区别(两个等号与三个等号)
    常用map总结
  • 原文地址:https://www.cnblogs.com/yangjing000/p/8227926.html
Copyright © 2011-2022 走看看