A SocketIO server created using aiohttp appends a new string to a global dict zoo
on receiving a addAnimal
message from its socketio clients.
Is it possible to also print out the dict zoo
every 5 seconds? My current code below does not seem to print zoo
every 5 seconds.
import time
from aiohttp import web
import socketio
zoo = []
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
@sio.on('addAnimal')
async def onAddAnimal(sid, animal):
zoo.append(animal)
print('Zoo: ', zoo)
if __name__ == '__main__':
web.run_app(app)
while True:
print('Zoo:', zoo)
time.delay(5)
Try this.
from aiohttp import web
import socketio
zoo = []
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
async def print_zoo(interval: int):
while True:
print(zoo)
await asyncio.sleep(interval)
@sio.on('addAnimal')
async def onAddAnimal(sid, animal):
zoo.append(animal)
print('Zoo: ', zoo)
if __name__ == '__main__':
asyncio.ensure_future(print_zoo(5))
web.run_app(app)