Queue not work as exected #15953
-
hi i get stuck with queue server on the ESP , working fast and fine. ESP32 (Wroom) code (running on micropython v1.23.0) import time, asyncio
from primitives import Queue
class server_task():
def __init__ (self):
self.bbb = 'test text'
self.q = Queue()
async def test(self,reader, writer):
print (self.bbb,'main')
print ('server start')
data = await reader.read(100)
print ('data IN:', data.decode())
await self.q.put(data.decode())
a = str('i am batman, not a return-server').encode()
writer.write(a)
await writer.drain()
writer.close()
await writer.wait_closed()
async def run_server(self):
server = await asyncio.start_server(self.test,'192.168.1.160',18888)
async with server: await server.task
def get (self):
print ('here get')
ret = self.q.get()
print ('get Queue: ',ret)
return('return data')
async def main():
cc = server_task()
asyncio.create_task(cc.run_server())
while True:
await asyncio.sleep(0.5)
h = cc.get()
print ('return get_in_loop: ',h)
print ('loop')
asyncio.run(main()) ESP CLI output
can someone tell me where my mistake is BR |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can edit your code like this: class server_task:
async def main(): asyncio.run(main()) The get() method in the server_task class is now an async def, and it awaits the self.q.get() method to fetch data from the queue properly. |
Beta Was this translation helpful? Give feedback.
-
Thanks a Lot ! :) it finally working :D i have in main loop put a wait_for so the loop is steady running try:
h = await asyncio.wait_for(cc.get(), timeout=0.5)
except :
print('timeout!') BR |
Beta Was this translation helpful? Give feedback.
You can edit your code like this:
import time, asyncio
from primitives import Queue
class server_task:
def init(self):
self.bbb = 'test text'
self.q = Queue()