现在我有一些代码如下所示:
userinput1 = abc.....
userinput2 = abc.....
userinput3 = abc.....
async def task1():
do something with userinput1...
do another thing...
async def task2():
do something with userinput2...
do another thing...
async def task3():
do something with userinput3...
do another thing...
async def main():
await asyncio.wait([task1() , task2(), task3()])
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
正如您在上面看到的,我有3个异步函数,它们可以同时完成不同的任务。我想知道是否有任何方法可以根据用户输入轻松创建许多函数?基本上,我希望它能够做到这一点:
userinput1 = abc.....
userinput2 = abc.....
userinput3 = abc.....
userinput4 = abc.....
amount_of_needed_functions = 4
一旦获得数据,它就会像下面的脚本一样运行:
async def task1():
do something with userinput1...
do another thing...
async def task2():
do something with userinput2...
do another thing...
async def task3():
do something with userinput3...
do another thing...
async def task4():
do something with userinput4...
do another thing...
async def main():
await asyncio.wait([task1() , task2(), task3(), task4()])
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
因此,它会使函数基于特定的可验证性(如userinput1),然后执行多次指定的操作(所需函数的数量),然后同时运行所有这些函数。抱歉,这是一个令人困惑的问题,但我完全不知道从哪里开始研究这个问题。谢谢
将用户输入作为参数传递给每个任务:
用于多任务的单一功能
多功能多任务
使用字典为每个输入调用所需的方法
如果所有函数都执行相同的操作,则可以尝试类似的操作
相关问题 更多 >
编程相关推荐