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()

posted @ 2019-09-26 16:45  Alive_2020  阅读(240)  评论(1编辑  收藏  举报