我正在用 python 撰寫代碼,我只想讓一個函式在time.sleep(). 但我找不到辦法。我的代碼:
from time import sleep
a = int()
def calc(a,b):
while True:
a=a*b
if a >> 12:
sleep(12)
#i just want this func to sleep here.
def print(msg):
while True:
msg = a
print(msg)
#i don't want this func to sleep
我該怎么辦?
uj5u.com熱心網友回復:
使用異步
import asyncio
async def calc(a,b):
while True:
a=a*b
if a >> 12:
await asyncio.sleep(12)
uj5u.com熱心網友回復:
我認為您需要閱讀一些有關該功能的資訊time.sleep。以下是檔案對此的說明:
暫停執行呼叫執行緒給定的秒數。引數可以是一個浮點數,以指示更精確的睡眠時間。實際的暫停時間可能比請求的少,因為任何捕獲的信號都會在執行該信號的捕獲例程后終止 sleep()。此外,由于系統中其他活動的調度,暫停時間可能比任意數量的請求長。
如果您呼叫它,該函式會在所需的位置暫停程式。它不會停止其他功能。
uj5u.com熱心網友回復:
f1() 函式中的代碼將在運行代碼 2 秒后執行,但在這段時間內 f2() 函式將被執行。
from time import time, sleep
def f1():
print("Function1")
def f2():
print("Function2")
t1 = time() # Stores current system time in seconds
print(t1)
time_to_delay_f1 = 2
while True:
if time()-t1 > time_to_delay_f1: # time()-t1 is the time passed after assigning t1 = time(), [see before 2 lines]
f1()
else:
f2()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/376180.html
