How to make sanic self restart

I’m running sanic with gunicorn, and nats.io

Sometime i got this error and how can i make it self-restart or making gunicorn auto restart the workers ?

Exception ignored in: <generator object Client.subscribe.<locals>.wait_for_msgs at 0x7fa6c47d3a50>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/nats/aio/client.py", line 721, in wait_for_msgs
  File "/usr/local/lib/python3.7/asyncio/queues.py", line 161, in get
  File "uvloop/loop.pyx", line 1250, in uvloop.loop.Loop.call_soon
  File "uvloop/loop.pyx", line 603, in uvloop.loop.Loop._call_soon
  File "uvloop/loop.pyx", line 607, in uvloop.loop.Loop._call_soon_handle
  File "uvloop/loop.pyx", line 636, in uvloop.loop.Loop._check_closed
RuntimeError: Event loop is closed
Exception ignored in: <generator object Client.subscribe.<locals>.wait_for_msgs at 0x7fa6c47d3b50>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/nats/aio/client.py", line 721, in wait_for_msgs
  File "/usr/local/lib/python3.7/asyncio/queues.py", line 161, in get
  File "uvloop/loop.pyx", line 1250, in uvloop.loop.Loop.call_soon
  File "uvloop/loop.pyx", line 603, in uvloop.loop.Loop._call_soon
  File "uvloop/loop.pyx", line 607, in uvloop.loop.Loop._call_soon_handle
  File "uvloop/loop.pyx", line 636, in uvloop.loop.Loop._check_closed
RuntimeError: Event loop is closed

btw, what is the correct way to keep my nats subscribers running on sanic, their example give not much

nc = NATS()

await nc.connect(servers=["nats://demo.nats.io:4222"])

future = asyncio.Future()

async def cb(msg):
  nonlocal future
  future.set_result(msg)

await nc.subscribe("updates", cb=cb)
await nc.publish("updates", b'All is Well')
await nc.flush()

# Wait for message to come in
msg = await asyncio.wait_for(future, 1)

If you are trying to debug the code, you should start the gunicorn by passing --reload parameter. It will cause workers to be restarted whenever application code changes.

I don’t know how to make gunicorn restart the workers automatically in production, but I use supervisor to manage the gunicorn process.

The gunicorn document also mentions it, see: http://docs.gunicorn.org/en/latest/deploy.html#supervisor

also see: Supervisor document

1 Like

yeah I mean autorestart sanic when the loop got crashed…

Try supervisor then. supervisor will auto restart gunicorn’s worker when the process is killed or crashed.

1 Like

Oh, you mean when the loop is closed. Then I don’t know the supervisor is able to do that.

Looks like an issue with nats, am I wrong?

Can’t you just wrap it in a try ... except ...?

about the error: Yeah, I break the subcriber onto another Go modules to run it.
about the subcriber: I still dont know how to implement it in sanic to get messsage continuously