【发布时间】:2021-07-20 12:56:23
【问题描述】:
这是我的代码的简化版本,但问题是一样的:
import asyncio
example = ["1", "2", "3", "4", "5"]
def test_1(numbers):
while True:
print(numbers)
tasks = []
async def test_2():
for numbers in example:
try:
tasks.append(test_1(numbers))
except:
pass
try:
await asyncio.gather(*tasks)
except:
pass
asyncio.run(test_2())
我想做的是在一个while循环中同时打印所有项目,但是当我运行代码时它只打印第一个项目,因为它是循环的,有没有办法修复它?
【问题讨论】:
-
该函数需要
await某处以暂停并允许其他代码运行,或者您需要实际上使用线程并行运行它们。 -
如何线程化使用不同项目的相同函数?我应该使用不同的参数吗?
标签: python loops while-loop python-requests python-asyncio