zoukankan      html  css  js  c++  java
  • 进程 ---线程 --协程的混合

    举例:

    from gevent import monkey; monkey.patch_all()
    import gevent
    import multiprocessing
    import threading
    from multiprocessing import current_process
    import time
    
    
    def gevent_handle():
        print ("33[1;33;42m now,begin handle gevent %s task.......33[0m"%threading.current_thread().getName())
        time.sleep(2)
        print("33[1;31;42m now,end handle gevent %s task.......33[0m"%threading.current_thread().getName())
    
    def thread_handle():
        print("now,begin handle thread %s task........."%threading.current_thread().getName())
        g=gevent.spawn(gevent_handle)
        g.join()
        print("now,end handle thread %s task........."%threading.current_thread().getName())
    def process_handle():
        print ("now,begin handle process %s task........."%current_process().name)
        t_list = []
        for i in range(5):
            t = threading.Thread(target=thread_handle,)
            t.start()
            t_list.append(t)
        for i in t_list:
            i.join()
        print("now,end handle process %s task........."%current_process().name)
    if __name__ == '__main__':
        p_list = []
        for i in range(2):
            p = multiprocessing.Process(target=process_handle,)
            p.start()
            p_list.append(p)
        for i in p_list:
            i.join()
        print ("now,every process task handle complete.........")

    结果:

    now,begin handle process Process-1 task.........
       now,begin handle thread Thread-1 task.........
          now,begin handle gevent DummyThread-3 task.......
       now,begin handle thread Thread-2 task.........
          now,begin handle gevent DummyThread-5 task.......
       now,begin handle thread Thread-4 task.........
          now,begin handle gevent DummyThread-7 task.......
       now,begin handle thread Thread-6 task.........
          now,begin handle gevent DummyThread-9 task.......
       now,begin handle thread Thread-8 task.........
          now,begin handle gevent DummyThread-10 task.......
    now,begin handle process Process-2 task.........
       now,begin handle thread Thread-1 task.........
          now,begin handle gevent DummyThread-3 task.......
       now,begin handle thread Thread-2 task.........
          now,begin handle gevent DummyThread-5 task.......
       now,begin handle thread Thread-4 task.........
          now,begin handle gevent DummyThread-7 task.......
       now,begin handle thread Thread-6 task.........
          now,begin handle gevent DummyThread-9 task.......
       now,begin handle thread Thread-8 task.........
          now,begin handle gevent DummyThread-10 task.......
          now,end handle gevent DummyThread-3 task.......
          now,end handle gevent DummyThread-5 task.......
          now,end handle gevent DummyThread-7 task.......
          now,end handle gevent DummyThread-9 task.......
          now,end handle gevent DummyThread-10 task.......
       now,end handle thread Thread-1 task.........
       now,end handle thread Thread-2 task.........
       now,end handle thread Thread-4 task.........
       now,end handle thread Thread-6 task.........
       now,end handle thread Thread-8 task.........
    now,end handle process Process-1 task.........
          now,end handle gevent DummyThread-3 task.......
          now,end handle gevent DummyThread-5 task.......
          now,end handle gevent DummyThread-7 task.......
          now,end handle gevent DummyThread-9 task.......
          now,end handle gevent DummyThread-10 task.......
       now,end handle thread Thread-1 task.........
       now,end handle thread Thread-2 task.........
       now,end handle thread Thread-4 task.........
       now,end handle thread Thread-6 task.........
       now,end handle thread Thread-8 task.........
    now,end handle process Process-2 task.........
    now,every process task handle complete.........

  • 相关阅读:
    图像处理国际会议
    [2015更新]用Word2007写CSDN博客
    【超详细教程】使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结
    奇异秀App:奇异秀秀奇异,用大头视频来拜年
    通俗讲解傅里叶级数
    LIBSVM的使用方法
    VC6.0的工程设置解读Project--Settings
    HOG:从理论到OpenCV实践
    如何在 Kaggle 首战中进入前 10%
    linux学习(2)
  • 原文地址:https://www.cnblogs.com/mmyy-blog/p/9465271.html
Copyright © 2011-2022 走看看