Python 如何每2分钟为一个数字加1?
如何每2分钟向变量添加1,直到变量达到100Python 如何每2分钟为一个数字加1?,python,Python,如何每2分钟向变量添加1,直到变量达到100 2 min later 0/100 ------> 1/100 程序将从0开始计算数字,并在每2分钟内每1个数字相加一次,直到达到100 2 min later 0/100 ------> 1/100 使用时间模块的睡眠功能 从时间导入睡眠 i=0 而我使用时间模块的睡眠功能 从时间导入睡眠 i=0 而我如果你需要制作进度条,你也可以检查 从TQM导入TQM
2 min later
0/100 ------> 1/100
程序将从0开始计算数字,并在每2分钟内每1个数字相加一次,直到达到100
2 min later
0/100 ------> 1/100
使用时间模块的睡眠功能
从时间导入睡眠
i=0
而我使用时间模块的睡眠功能
从时间导入睡眠
i=0
而我如果你需要制作进度条,你也可以检查
从TQM导入TQM
导入时间
对于tqdm(范围(100))中的uu:
时间。睡眠(120)
如果需要制作进度条,也可以进行检查
从TQM导入TQM
导入时间
对于tqdm(范围(100))中的uu:
时间。睡眠(120)
单线解决方案:
from time import sleep
for t in range(100): time.sleep(120)
单线解决方案:
from time import sleep
for t in range(100): time.sleep(120)
我用睡眠
from time import sleep
for i in range(100):
sleep(120)
# print(i)
我用睡眠
from time import sleep
for i in range(100):
sleep(120)
# print(i)
我相信到目前为止提出的所有解决方案都是线程锁定
import asyncio, time
async def waitT(tWait, count):
print(count)
while count < 100: #The 100 could be passed as a param to make it more generic
await asyncio.sleep(tWait)
count = count + 1
print(count)
return
async def myOtherFoo():
#Do stuff in here
print("aaa")
await asyncio.sleep(2)
print("test that its working")
return
async def main(count):
asyncio.gather(myOtherFoo(), waitT(120, count)) #Obviously tweak 120 to whatever interval in seconds you want
return
if __name__ == "__main__":
count = 0
asyncio.create_task(main(count))
导入异步IO,时间
异步def waitT(tWait,计数):
打印(计数)
当count<100时:#可以将100作为参数传递,使其更通用
等待异步睡眠(tWait)
计数=计数+1
打印(计数)
回来
异步定义myOtherFoo():
#在这里做事
打印(“aaa”)
等待异步睡眠(2)
打印(“测试其工作情况”)
回来
异步def主(计数):
asyncio.gather(myOtherFoo(),wait(120,count))#显然,可以将120调整为您想要的任何时间间隔
回来
如果名称=“\uuuuu main\uuuuuuuu”:
计数=0
asyncio.create_任务(主(计数))
一个简单的、希望可读的异步解决方案。不检查运行循环等-但应该为您的计数器中的每个更新之间的操作打开一系列可能性。我相信目前提供的所有解决方案都是线程锁定
import asyncio, time
async def waitT(tWait, count):
print(count)
while count < 100: #The 100 could be passed as a param to make it more generic
await asyncio.sleep(tWait)
count = count + 1
print(count)
return
async def myOtherFoo():
#Do stuff in here
print("aaa")
await asyncio.sleep(2)
print("test that its working")
return
async def main(count):
asyncio.gather(myOtherFoo(), waitT(120, count)) #Obviously tweak 120 to whatever interval in seconds you want
return
if __name__ == "__main__":
count = 0
asyncio.create_task(main(count))
导入异步IO,时间
异步def waitT(tWait,计数):
打印(计数)
当count<100时:#可以将100作为参数传递,使其更通用
等待异步睡眠(tWait)
计数=计数+1
打印(计数)
回来
异步定义myOtherFoo():
#在这里做事
打印(“aaa”)
等待异步睡眠(2)
打印(“测试其工作情况”)
回来
异步def主(计数):
asyncio.gather(myOtherFoo(),wait(120,count))#显然,可以将120调整为您想要的任何时间间隔
回来
如果名称=“\uuuuu main\uuuuuuuu”:
计数=0
asyncio.create_任务(主(计数))
一个简单的、希望可读的异步解决方案。不检查运行循环等-但应为您的计数器中每次更新之间的操作打开一系列可能性。导入时间然后对于范围内的j(100):
然后时间。睡眠(120)
然后打印(j,“/100”)
。就这些了这能回答你的问题吗<代码>导入时间
然后对于范围(100)内的j:
然后时间。睡眠(120)
然后打印(j,“/100”)
。就这些了这能回答你的问题吗?