zoukankan      html  css  js  c++  java
  • python并发编程-多进程与多协程

    # -*- coding: utf-8 -*-
    __author__ = 'jjy'
    __Software__ = 'PyCharm'
    
    from ThriftClient import get_sequence_service_thrift
    from multiprocessing import Process
    import gevent
    from gevent import monkey
    
    monkey.patch_all()
    
    
    def process_start(i):
        try:
            _file = open('./test_' + str(i) + '.txt', 'a')
            print('start for `````````````````', i)
            for j in range(1000):
                p = gevent.spawn(get_sequence_service_thrift, _file)
                p.join()
        except Exception as e:
            print(e)
        finally:
            print('close `````````````````', i)
            _file.close()
    
    
    def task_start():
        import time
        start_time = time.time()
        # while True:
        # for i in range(3):
        for i in range(1, 3):  # 2
            p = Process(target=process_start, args=(i,))
            p.start()
        p.join()
        print('----end----')
        end_time = time.time()
        print(end_time)
        print('Total_time: %s' % str(end_time - start_time))
        avg_time = 2000 / (end_time - start_time)
        print('Avg %f / s ````````````````````````````````````' % avg_time)
    
    
    if __name__ == '__main__':
        task_start()
    
    
  • 相关阅读:
    d
    今天刚注册,测试下
    关于Hadoop的简单介绍
    leveldb 阅读笔记 (2) 简易测试框架
    leveldb 阅读笔记(1) 内存分配器 Arena
    数论
    伴随网站
    convenience website
    大佬独特的骗分技巧
    动态规划
  • 原文地址:https://www.cnblogs.com/sunxiuwen/p/11592745.html
Copyright © 2011-2022 走看看