• python multiprocessing 多进程


      '''
      如果要启动大量的子进程,可以用进程池的方式批量创建子进程:
      '''
      def test_task(name):
        print 'Run task %s (%s)...' % (name, os.getpid())
        start = time.time()
        time.sleep(random.random() * 3)
        end = time.time()
        print 'Task %s runs %0.2f seconds.' % (name, (end - start))

      if __name__=='__main__':
        print 'Parent process %s.' % os.getpid()
        p = Pool()
        for i in range(5):
        p.apply_async(test_task, args=(i,))
        print 'Waiting for all subprocesses done...'
        p.close()
        p.join()
        print 'finish'

               对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。

        '''
        以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据:
        '''
        #写数据
        def write(queue):
          for i in range(1,10):
            print 'put %d to queue.'% i
            queue.put(i)
            time.sleep(random.random())

        #读数据
        def read(queue):
          while True:
          i = queue.get(True)
          print 'get %d from queue.'%i

        

        #测试代码
        def test():
          q = Queue()#创建队列,并传给子进程
          WriteProcessor = Process(target = write,args=(q,))
          ReadProcessor = Process(target = read,args=(q,))

          #启动写进程,写入数据
          WriteProcessor.start()

          #启动读进程,读取数据
          ReadProcessor.start()

          #等待WriteProcessor结束
          WriteProcessor.join()
               

          #终止读进程
          ReadProcessor.terminate()

         参考资料:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868323401155ceb3db1e2044f80b974b469eb06cb43000

  • 相关阅读:
    JS 这一次彻底理解选择排序
    JS 这一次彻底理解冒泡排序
    JS script脚本async和defer的区别
    精读JavaScript模式(九),JS类式继承与现代继承模式其二
    google recaptcha 谷歌人机身份验证超详细使用教程,前端/后端集成说明
    JS 究竟是先有鸡还是有蛋,Object与Function究竟谁出现的更早,Function算不算Function的实例等问题杂谈
    NetFramework 专栏总集篇
    详解 服务发现 的基本实现
    CF150E Freezing with Style(点分治)
    LOJ6032.「雅礼集训 2017 Day2」水箱
  • 原文地址:https://www.cnblogs.com/shaosks/p/5627052.html
Copyright © 2020-2023  润新知