Python多线程使用方法
1:基本使用方法
import threading
import time
def a():
for secs in range(14, 0, -1):
print('A倒计时:', secs)
time.sleep(1)
def b():
for secs in range(14, 0, -1):
print('B倒计时:', secs)
time.sleep(1)
t = threading.Thread(target=a)
s = threading.Thread(target=b)
t.start()
s.start()
2: 遇到的坑【asyncio - await】 async await 内使用办法
async def saySorry():
for secs in range(14, 0, -1):
print('倒计时:', secs)
time.sleep(1)
def between_callback():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(saySorry())
t = threading.Thread(target=between_callback)
t.start()
|