sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

queue.put faild with shared_ctx

muyu525 opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

run app:
python -m sanic aaa.app --workers=4

  1. Add a multiprocessing.Queue to shared_ctx
  2. make a request, then call queue.put(1)

then get error:

http: LogLevel.ERROR: ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) while doing a GET request to URL: http://127.0.0.1:8000/

Press Ctrl + C , a worker killed;

[14399] [INFO] Starting worker [14399]
[14400] [INFO] Starting worker [14400]
[14402] [INFO] Starting worker [14402]
[14401] [INFO] Starting worker [14401]
[14390] [INFO] Received signal SIGINT. Shutting down.
[14390] [INFO] Server Stopped
[14400] [INFO] Stopping worker [14400]
[14399] [INFO] Stopping worker [14399]
[14402] [INFO] Stopping worker [14402]

Code snippet

from multiprocessing import Queue

from sanic import Sanic
from sanic.response import text


app = Sanic(__name__)


@app.get('/')
async def index(request):
    request.app.shared_ctx.queue.put(1)
    return text('Hello')


@app.main_process_start
async def main_start(app):
    app.shared_ctx.queue = Queue()



if __name__ == '__main__':
    app.run(host='localhost', port=8000, workers=4)

How do you run Sanic?

Sanic CLI

Operating System

Archlinux

Sanic Version

23.3.0

image

default:
The Main process is running with ForkContext, and WorkerProcesses are running with SpawnContext.

And this will work

from sanic import Sanic

Sanic.start_method = "fork"  # make WorkerProcess using ForkContext