Python uasyncio parralel(多线程)似乎无法工作
我尝试使用两个例程,我想在同一时间连续执行,但只有第一个正在运行 我错在哪里Python uasyncio parralel(多线程)似乎无法工作,python,python-asyncio,micropython,Python,Python Asyncio,Micropython,我尝试使用两个例程,我想在同一时间连续执行,但只有第一个正在运行 我错在哪里 async def screen(self): #first routine Writer.set_textpos(ssd, 0, 0) # In case previous tests have altered it wri = Writer(ssd, small, verbose=False) wri.set_clip(False, False, False) ...
async def screen(self): #first routine
Writer.set_textpos(ssd, 0, 0) # In case previous tests have altered it
wri = Writer(ssd, small, verbose=False)
wri.set_clip(False, False, False)
...
refresh(ssd)
utime.sleep_ms(200)
async def InvPress(self): #second routine
utime.sleep_ms(1000)
print('Sw2',self.SW2.value())
while True:
print(self.SW2.value())
if self.SW2.value():
if self.PressPos == 1:
self.stepper.steps(-1600,2600)
self.PressPos = not self.PressPos
else :
self.stepper.steps(+1600,2600)
self.PressPos = not self.PressPos
utime.sleep_ms(120)
async def main(self): #this don't work in parallel (only the first routine is running)
uasyncio.create_task(self.InvPress())
uasyncio.create_task(self.screen())
await uasyncio.sleep(10)
(免责声明:我不知道微型蟒蛇)
异步程序基于所谓的协同调度。只能在某些点切换协同程序/任务。这些点是代码中的wait
s
如果协同程序中没有wait
,则无法切换任务,即没有其他协同程序有机会运行。这就是InvPress
的情况。(另一个协同程序未完全列出)
与此密切相关的规则是,异步程序中的常规睡眠是错误的。它除了睡眠之外什么也不做,并阻止其他协同程序在此期间运行。在这种睡眠过程中,程序完全死机(无响应)。如果需要延迟,请始终等待适当的异步睡眠例程