简体   繁体   English

使用 Multiprocessing 或 Threading 或 Asyncio 进行多任务处理,具体取决于场景

[英]Multi-tasking with Multiprocessing or Threading or Asyncio, depending on the Scenario

I have my codes ready for 1 at a time performance, I wanna upgrade it to something fancy, multi-tasking.我已准备好一次执行 1 个代码,我想将其升级为花哨的多任务处理。 I am seeking helps about what I can use to achieve my goal.我正在寻求有关我可以用来实现目标的帮助。

my codes performs in this order: parsing multi-pages, parsing multi-posts, parsing multi-images.我的代码按以下顺序执行:解析多页、解析多篇文章、解析多图像。 I tried to do multi-pages with multi-processing with pool.map(), it came out with KeyError of Daemonic can't have children processes.我尝试使用 pool.map() 进行多页处理,结果出现了 Daemonic 的 KeyError 不能有子进程。 My understanding of this multi-tasking procedure is that parsing pages are fast, parsing posts and images can be really long.我对这个多任务程序的理解是解析页面很快,解析帖子和图像可能很长。

What if I do parsing posts and parsing images together on single page, can it be allowed?如果我在单个页面上一起解析帖子和解析图像怎么办,是否允许?

Which modules should i use to do so?我应该使用哪些模块来做到这一点? thread?线? multiprocessing?多处理? asyncio?异步? I went through a lot lately, I am struggling with what I should use.我最近经历了很多,我正在为我应该使用的东西而苦苦挣扎。

So off the top of my head you can look at 2 things.所以在我的脑海里,你可以看到两件事。

1) Asyncio (be careful this example uses threading and is not thread safe specifically the function asyncio.gather) 1)Asyncio(注意这个例子使用线程并且不是线程安全的,特别是函数asyncio.gather)

import asyncio
for work in [1,2,3,4,5]:
    tasks.append(method_to_be_called(work))

results = await asyncio.gather(*tasks)

2) Asyncio + multiprocessing https://github.com/jreese/aiomultiprocess 2) Asyncio + 多处理https://github.com/jreese/aiomultiprocess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM