python

进程、通讯、池

文章暂存

systemime
2020-05-17
1 min

摘要.

from multiprocessing import Process, Queue, Pool
import time, datetime
import os, asyncio


def xxx(*args, **kwargs):
    # print(args, kwargs)
    data = {'data': [args[1], args[0]]}
    q.put(data)
    print("这里是P1进程,PID:%s" % os.getpid())


def qqq(*args, **kwargs):
    # print(args, kwargs)
    print("这里是P2进程,PID:%s" % os.getpid())
    while True:
        print(q.get(True))


async def www():
    print(os.getpid())
    await time.sleep(3)
    print(123123)


if __name__ == "__main__":
    # q = Queue()
    # pp = Pool(5)
    # for i in range(5):
    #     pp.apply_async(xxx, args=('张三', i))
    # pp.close()
    # p2 = Process(target=qqq, args=('李四',), kwargs={'abc': 456})
    # p2.start()
    # p2.join()
    # pp.join()
    www()

线程池

1)引入threadpool 模块
(2)定义线程函数
(3)创建线程池threadpool.ThreadPool()4)创建需要线程池处理的任务即threadpool.makeRequests()5)将创建的多个任务put 到线程池中,threadpool.putRequest
(6)等到所有任务处理完毕theadpool.pool()
上次编辑于: 2021/5/20 下午3:26:49