如何压缩根据输入选择多少并发任务的代码?

2024-06-01 10:10:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个scraper项目,可以处理异步请求库和trio。 我想选择多少并发任务是基于输入,但我的代码是长的和原始的

我对并发任务使用trio的产卵和育婴对象(docs:https://trio.readthedocs.io/en/latest/reference-core.html

这是我草率的代码:

import trio
import asks
Number_of_workers = input("how many workers do you want?: ") #How many tasks I want between 1 and 5

async def child1(s):
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def child2():
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def child3():
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def child4():
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def child5():
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def parent(): 
    s = Session(connections=5)
    async with trio.open_nursery() as nursery:
        if int(Number_of_workers) == 1:
            nursery.start_soon(child1, s)

        elif int(Number_of_workers) == 2:
            nursery.start_soon(child1, s)
            nursery.start_soon(child2, s)

        elif int(Number_of_workers) == 3:
            nursery.start_soon(child1, s)
            nursery.start_soon(child2, s)
            nursery.start_soon(child3, s)

        elif int(Number_of_workers) == 4:
            nursery.start_soon(child1, s)
            nursery.start_soon(child2, s)
            nursery.start_soon(child3, s)
            nursery.start_soon(child4, s)

        elif int(Number_of_workers) == 5:
            nursery.start_soon(child1, s)
            nursery.start_soon(child2, s)
            nursery.start_soon(child3, s)
            nursery.start_soon(child4, s)
            nursery.start_soon(child5, s)
trio.run(parent)

我想你可以理解我的意思,这个示例代码在理论上是有效的,但是对于一些可能被减少到更少代码行的东西来说,它是非常长的

这种方案在处理10或20个工人时会特别长,并且总是限制在预定义的数量内

在其内部,每个子级都是相同的、相同的代码,它只是从带有importlib的外部module.py文件中获取不同的数据(例如参数和url)

有没有一种方法可以把它简化成一个更优化的代码


Tags: of代码httpsnumberasynctrioexampledef
1条回答
网友
1楼 · 发布于 2024-06-01 10:10:40

你可以使用循环

async def child(s):
    r = await s.get("https://example.com", params={"example":"example"})
    print("do something with", r.text)

async def parent(): 
    s = Session(connections=5)
    async with trio.open_nursery() as nursery:
        for i in range(Number_of_workers):
            nursery.start_soon(child, s)

编辑:这里有一个自包含的演示,您可以运行它来让自己相信它实际上运行的是并发任务。它还演示了如何将不同的参数值传递给不同的任务,以便它们执行不同的操作—在本例中,打印不同的消息:

import trio

Number_of_workers = 10

async def child(i):
    print("child {}: started".format(i))
    await trio.sleep(5)
    print("child {}: finished".format(i))

async def parent():
    async with trio.open_nursery() as nursery:
        for i in range(Number_of_workers):
            nursery.start_soon(child, i)

trio.run(parent)

试试看

相关问题 更多 >