簡體   English   中英

Asyncio和multiprocessing.Process-如何傳遞協程

[英]Asyncio and multiprocessing.Process- how to pass a coroutine?

我有一個協程,它正在抓取一些數據。 我正在嘗試添加多處理以加快速度。 我想出了使用multiprocesesing.Process。 我的協程叫做fetch_students_pages

我想做的是將此協程作為目標傳遞給Process類:

    def fetch_students_concurrently(self):
        for _ in range(10):
            Process(target=self.fetch_students_pages).start()

但如果失敗,則:

/usr/lib/python3.7/multiprocessing/process.py:99:RuntimeWarning:協程'StudentPageParser.fetch_students_pages'從未等待過self._target(* self._args,** self._kwargs)RuntimeWarning:啟用tracemalloc來獲取對象分配回溯

有沒有辦法等待它或改用其他解決方案?

考慮使用map()代替:

cores = 8

with Pool(cores) as p:
    p.map(self.fetch_students_pages, range(cores))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM