问题描述
我正在使用 aioaria2 websocket 客户端,它可以使用该功能作为下载开始或完成时的通知。根据代码,处理通知的函数是用 asyncio.create_task
创建的,它在后台永远循环。我需要种子文件,因为 aioaria2 还没有提供,所以我正在使用 asyncio.create_subprocess_exec
,因为我不需要该 SeedFile 函数的返回值,我用 asyncio.create_task
开始工作,它应该为我节省大量时间而无需等待那份工作。
async def seedFile(self,file: util.aria2.Download) -> None:
file_path = Path(str(file.dir / file.info_hash) + ".torrent")
if not file_path.is_file():
return
self.log.info(f"seeding: [gid: '{file.gid}']")
port = util.aria2.get_free_port()
cmd = [
"aria2c","--enable-rpc","--rpc-listen-all=false",f"--rpc-listen-port={port}","--bt-seed-unverified=true","--seed-ratio=1",f"-i {str(file_path)}"
]
try:
_,stderr,ret = await util.system.run_command(*cmd)
except Exception as e: # skipcq: PYL-W0703
self.log.warning(e)
return
if ret != 0:
self.log.info("seeding: [gid: '{file.gid}'] - Failed")
self.log.warning(stderr)
return
self.log.info(f"seeding: [gid: '{file.gid}'] - Complete")
但是一旦它在 await util.system.run_command
上,任务总是挂起并且永远不会运行,如果我等待另一个工作,它会像死锁一样卡住,但任务甚至没有运行。是不是因为 create_subprocess_exec 不能在后台运行?
async def listen(self) -> None:
try:
while not self.closed:
try:
data = await self.client_session.receive_json(loads=self.loads)
except TypeError: # aria2抽了
continue
if not data or not isinstance(data,dict):
continue
asyncio.create_task(self.handle_event(data))
finally:
await self.close()
handle_event
async def handle_event(self,data: dict) -> None:
if "result" in data or "error" in data:
ResultStore.add_result(data)
# if "result" in self.functions:
# await asyncio.gather(*map(lambda x: x(self,future),self.functions["result"]))
if "method" in data:
method = data["method"]
if method in self.functions: # Todo 有鬼
await asyncio.gather(*map(lambda x: x(self,data),self.functions[method]))
我下载完成后seedFile的功能
async def onDownloadComplete(self,client: Aria2WebsocketClient,data: Union[Dict[str,Any],Any]) -> None:
gid = data["params"][0]["gid"]
file = await self.getDownload(client,gid)
if file.bittorrent:
self.loop.create_task(self.seedFile(file),name=f"Seed-{file.gid}")
我无法在下载时播种(默认行为 aria2),因为下载完成后会导致一些不好的问题,因为文件状态在种子完成之前仍然处于活动状态,所以我的解决方法是在下载时不播种,然后在下载完成后播种在 asyncio.create_subprocess_exec
的后台,但正如我在开始时所说的那样,无论我等待多少,该任务始终处于待处理状态。 await
、asyncio.wait
、asyncio.wait_for
、asyncio.gather
都一样使调用者死锁或永远循环
问题
我如何在后台使用 asyncio.create_subprocess_exec
进行播种,而任务实际上正在运行?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)