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中运行不存在这个问题
  • 相关阅读:
    各种编译器
    C99特性
    动态内存分配
    MDK C++编程说明
    C++类的大小计算
    WPF DataGrid添加编号列
    WPF实现打印用户界面功能
    WPF DataGrid 导出Excel
    知识点总结
    Winfrom控件使用
  • 原文地址:https://www.cnblogs.com/yangjing000/p/8227926.html
Copyright © 2011-2022 走看看