Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run uvicorn with multiple workers on Windows - WinError 5 #484

Closed
grays820 opened this issue Nov 11, 2019 · 10 comments
Closed

Run uvicorn with multiple workers on Windows - WinError 5 #484

grays820 opened this issue Nov 11, 2019 · 10 comments

Comments

@grays820
Copy link

from starlette.applications import Starlette
from starlette.responses import JSONResponse
import uvicorn

app = Starlette(debug=True)

@app.route('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})

@app.route('/long')
async def longpool(request):
z = 0
for x in range(1000000):
z = z+1
print(z)
return JSONResponse({'hello11': 'world'})

if name == 'main':
uvicorn.run(app, host='0.0.0.0', port=8000,workers=2)

error:

�[33mWARNING�[0m: You must pass the application as an import string to enable 'reload' or 'workers'.
�[32mINFO�[0m: Uvicorn running on �[1mhttp://0.0.0.0:8000�[0m (Press CTRL+C to quit)
�[32mINFO�[0m: Started parent process [�[36m�[1m40920�[0m]
Traceback (most recent call last):
File "main.py", line 29, in
uvicorn.run(app, host='0.0.0.0', port=8000,workers=2)
File "C:\Users\grays\Envs\python3\lib\site-packages\uvicorn\main.py", line 310, in run
supervisor.run(server.run, sockets=[sock])
File "C:\Users\grays\Envs\python3\lib\site-packages\uvicorn\supervisors\multiprocess.py", line 59, in run
process.start()
File "c:\python37\Lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
File "c:\python37\Lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "c:\python37\Lib\multiprocessing\popen_spawn_win32.py", line 65, in init
reduction.dump(process_obj, to_child)
File "c:\python37\Lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'request_response..app'

(python3) C:\Users\grays\Documents\Workspace\MyProjects\AnyTest>Traceback (most recent call last):
File "", line 1, in
File "c:\python37\Lib\multiprocessing\spawn.py", line 99, in spawn_main
new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "c:\python37\Lib\multiprocessing\reduction.py", line 87, in steal_handle
_winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
PermissionError: [WinError 5] 拒绝访问。

already run cmd with Administrator mode

windows 10
uvicorn 0.10.4
starlette 0.12.13

@tomchristie
Copy link
Member

The initial warning is the relevant part here: "You must pass the application as an import string to enable 'reload' or 'workers'."

Really we ought to be quitting uvicorn, rather than allowing it to run to failure after that message. (And change it from a warning to an error.)

You'll need to use the following style instead...

# If the application is `app` in module `example.py` -> Use "example:app"
uvicorn.run("example.app", host='0.0.0.0', port=8000,workers=2) 

@tomchristie
Copy link
Member

Incidentally, are you seeing "�[0m" or proper terminal colours in the logs?

@grays820
Copy link
Author

The initial warning is the relevant part here: "You must pass the application as an import string to enable 'reload' or 'workers'."

Really we ought to be quitting uvicorn, rather than allowing it to run to failure after that message. (And change it from a warning to an error.)

You'll need to use the following style instead...

# If the application is `app` in module `example.py` -> Use "example:app"
uvicorn.run("example.app", host='0.0.0.0', port=8000,workers=2) 

tks,i change code like
uvicorn.run("main:app", host='0.0.0.0', port=8000,workers=2)
now it raise OSError: [WinError 10022]

TIM截图20191112094237

@tomchristie
Copy link
Member

Looks to me like this should be closed via #487. Now released as 0.10.6.

Let me know how that goes.

@grays820
Copy link
Author

grays820 commented Nov 13, 2019

Looks to me like this should be closed via #487. Now released as 0.10.6.

Let me know how that goes.

i upgrade to 0.10.8,the error change to [WinError 10022],orz..
TIM截图20191113101116

@ghostzero192
Copy link

ghostzero192 commented Nov 13, 2019

@grays820 try this workaround:

if __name__ == "__main__":
    uvicorn.run("main:app", host='127.0.0.1', port=8000, workers=8, debug=True)

it should work, aparently the debug is set to False by default and this is what is causing the error

@grays820
Copy link
Author

grays820 commented Nov 14, 2019

@grays820 try this workaround:

if __name__ == "__main__":
    uvicorn.run("main:app", host='127.0.0.1', port=8000, workers=8, debug=True)

it should work, aparently the debug is set to False by default and this is what is causing the error

yes,set debug=True,it work fine,why not support debug=False?

@cgi1
Copy link

cgi1 commented May 28, 2020

Getting the same error message when trying to start a FastAPI application here

@WhitehatLaksh
Copy link

The initial warning is the relevant part here: "You must pass the application as an import string to enable 'reload' or 'workers'."

Really we ought to be quitting uvicorn, rather than allowing it to run to failure after that message. (And change it from a warning to an error.)

You'll need to use the following style instead...

# If the application is `app` in module `example.py` -> Use "example:app"
uvicorn.run("example.app", host='0.0.0.0', port=8000,workers=2) 

Not working

@larpig
Copy link

larpig commented Oct 14, 2022

Hey @WhitehatLaksh ,

Indeed, such an approach it is not working. You should try the approach proposed on [BUG] uvicorn can not start FastAPI with example settings, which is:

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def get_root():
    return {"message": "Hello World"}


if __name__ == "__main__":
    uvicorn.run("__main__:app", host="0.0.0.0", port=8000, reload=True, workers=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants