如何使用python asyncio异步调度任务?

发布于 2025-01-15 10:08:55 字数 1670 浏览 7 评论 0原文

I am looking to implement a simple p2p file downloader in python. The downloader needs to work within the following limitations:

  • When queried, the tracker responds with exactly 2 peers
  • The tracker ignores requests made within 2 seconds of the previous request

The downloader should attempt to download a single file as fast as possible. From my reading about this I thought that I should be using asyncio. I thought I'd structure the code something like this pseudo code:

async def downloader(peer):
    while file is not downloaded:
        download a new block from the peer without blocking

response = synchronously query tracker for initial pair of peers
newPeers = exractPeers(response)
while file not downloaded:
    for peer in newPeers:
        dispatch new downloader(peer)
    wait 2 seconds without blocking downloaders
    response = query tracker for initial pair of peers without blocking downloaders
    newPeers = exractPeers(response)

What asyncio methods should I be using to "dispatch" new downloaders and query the tracker? From my testing it seems that awaiting an async function blocks the event loop:

import asyncio
from random import randint

async def myfunc(i):
    print("hello", i)
    await asyncio.sleep(randint(1,3))
    print("world", i)

async def main():
    await myfunc(1)
    await myfunc(2)
    await myfunc(3)
    await myfunc(4)
    
asyncio.run(main())

This code runs each myfunc call as though it were defined without async 。 I'd appreciate anything, including basic pointers.

I am looking to implement a simple p2p file downloader in python. The downloader needs to work within the following limitations:

  • When queried, the tracker responds with exactly 2 peers
  • The tracker ignores requests made within 2 seconds of the previous request

The downloader should attempt to download a single file as fast as possible. From my reading about this I thought that I should be using asyncio. I thought I'd structure the code something like this pseudo code:

async def downloader(peer):
    while file is not downloaded:
        download a new block from the peer without blocking

response = synchronously query tracker for initial pair of peers
newPeers = exractPeers(response)
while file not downloaded:
    for peer in newPeers:
        dispatch new downloader(peer)
    wait 2 seconds without blocking downloaders
    response = query tracker for initial pair of peers without blocking downloaders
    newPeers = exractPeers(response)

What asyncio methods should I be using to "dispatch" new downloaders and query the tracker? From my testing it seems that awaiting an async function blocks the event loop:

import asyncio
from random import randint

async def myfunc(i):
    print("hello", i)
    await asyncio.sleep(randint(1,3))
    print("world", i)

async def main():
    await myfunc(1)
    await myfunc(2)
    await myfunc(3)
    await myfunc(4)
    
asyncio.run(main())

This code runs each myfunc call as though it were defined without async. I'd appreciate anything, including basic pointers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文