方式一:使用 multiprocessing 库
from loguru import logger
import multiprocessing
def start_request(message: str) -> int:
try:
logger.debug(message)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=2)
for message in ['haha', 'hehe']:
pool.apply_async(start_request, (message,))
pool.close()
pool.join()
方式二:使用 concurrent.futures 的 ProcessPoolExecutor
from loguru import logger
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
def start_request(message: str) -> int:
try:
logger.debug(message)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
pool = ProcessPoolExecutor(
max_workers=2
)
for message in ['haha', 'hehe']:
pool.submit(start_request, message)
pool.shutdown(wait=True)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。