python并行编程实现

# -*- coding: utf-8 -*-
# @Time    : 2019/4/9 17:16
# @Author  : RIO
# @desc: threading并发编程

import threading
from time import ctime, sleep
from algorithm.basic_algorithm import quanpailie

loops = [4, 2]
res_list = []

class ThreadFunc(object):
    def __init__(self, func, args, name=''):
        self.name = name
        self.func = func
        self.args = args

    def __call__(self):
        self.func(*self.args)


def loop(nloop, nsec):
    print(nloop, 'start at', ctime())
    res = quanpailie.main1()
    res_list.append(res)
    print(nloop, 'end at', ctime())


def main():
    print('start at', ctime())
    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = threading.Thread(target=ThreadFunc(loop, (i, loops[i]), loop.__name__))
        threads.append(t)
    for i in nloops:
        threads[i].start()

    for i in nloops:
        threads[i].join()
    print(res_list)
    print('all end', ctime())


if __name__ == '__main__':
    main()

你可能感兴趣的:(python)