美文网首页
python并发执行demo

python并发执行demo

作者: bluepang | 来源:发表于2022-01-11 14:17 被阅读0次

    1、正常多线程-继承式调用

    import threading

    thread_max_num = threading.Semaphore(10)  # 设置最大线程数为10

    class MyThread(threading.Thread):

        def __init__(self, param):

            threading.Thread.__init__(self)

            self.param = param

        def run(self):

            with thread_max_num: # 设置最大线程数

                # 单个请求代码

                self.result = fuc(self.param)

        def get_result(self):

            try:

                  return self.result

            except Exception:

                return None

    if __name__ == '__main__':

        params = []

        ths = []

        result_list = []

        for param in params:

            th = MyThread(param)

            th.start()

            ths.append(th)

        for th in ths:

            result_list.append(th.get_result())

    2、正常多线程-直接调用

    import threading

    _result = []

    ths = []  # 初始化线程list

    params = []

    def run(param):

        global _result

        with thread_max_num: # 设置最大线程数           

            # 单个请求代码         

            result = None

            _result.append(result)

    for i in params:

        th = threading.Thread(target=run, args=(param, ))

        th.start()

        ths.append(th)

    for th in ths:

        th.join()

    print(_result)

    3、线程池

    from multiprocessing import Pool

    params = []

    def run(param):

        pass

    pool = Pool(10)  # 设置最大线程数为10

    ret = pool.map(run, params)

    4、协程-gevent

    from gevent.pool import Pool

    from gevent import monkey;monkey.patch_all()

    import gevent

    params = []

    def run(param):

        pass

    p = Pool(10)

    g_list = []

    for param in params:

        g = p.spawn(run, param)

        g_list.append(g)

    gevent.joinall(g_list)

    result_list = []

    for g in g_list:

        result_list.append(g.value)

    5、多进程-继承式调用

    from multiprocessing import Process

    class MyProcess(Process):

        def __init__(self):

            super().__init__(param)

            self.param = param

        def run(self):

            pass

    if __name__ == '__main__':

        params = []

        for param in params:

            pr = MyProcess(param)

            pr.start()

    6、多进程-直接调用

    from multiprocessing import Process

    def run(param):

        pass

    params = []

    for param in params:

        pr = Process(target=run, args=(param, ))

        pr.start()

    相关文章

      网友评论

          本文标题:python并发执行demo

          本文链接:https://www.haomeiwen.com/subject/tmsbcrtx.html