zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java 后台校验格式
    spring AOP 实现事务和主从读写分离
    【Day5】项目实战.CSDN热门文章爬取
    【Day5】3.反爬策略之模拟登录
    【Day5】2.反爬策略之代理IP
    【Day5】1.Request对象之Header伪装策略
    【Day4】5.Request对象之Http Post请求案例分析
    【Day4】4.Request对象之Get请求与URL编码
    【Day4】3.urllib模块使用案例
    【Day4】2.详解Http请求协议
  • 原文地址:https://www.cnblogs.com/shaosks/p/5627052.html
Copyright © 2011-2022 走看看