0

my goal is to send live notifications to the user.
the message will arrive from a celery worker.
and will be sent to the user using aiohttp through sockjs.

how can i run both on the same app ? or receive the messages on the aiohttp instance where i have data of the authenticated users in memory ? what is the best approach to achieve that ?

i have tried running them together using the aiohttp on_startup. but celery is blocking the main thread so its not possible.

async def run_celery(app):
    .... run celery
app = web.Application(loop=asyncio.get_event_loop())
app.on_startup.append(run_celery)
sockjs.add_endpoint(app, msg_handler, name='messeging', prefix='/sockjs/')

thank you very much.
shay

shay te
  • 1,028
  • 3
  • 12
  • 23
  • Keep your Celery workers and aiohttp-based web service separate. It makes no sense to have them all in one big application. Blocking code should be executed in a separate thread so it does not block the main event loop. – DejanLekic Oct 17 '19 at 08:00
  • @DejanLekic thank you for the comment. how can i make tasks run on the aiohttp side. where i have the needed data in memory ? – shay te Oct 17 '19 at 08:43
  • Your Celery task would be then extremely trivial - an HTTP request to your aiohttp-based service, and return whatever was returned by the web-service. – DejanLekic Oct 17 '19 at 09:09
  • thanks again @DejanLekic. if i understand correctly the celery worker should notify aiohttp server using http request ? (the aiohttp need to push data to the client) – shay te Oct 17 '19 at 09:36

0 Answers0