villekr / ocpp-asgi

ocpp-asgi extends ocpp library to provide ASGI compliant interface for implementing OCPP Central System.
MIT License
20 stars 4 forks source link

Error while setting workers #2

Closed ChaitanyaYeole02 closed 2 years ago

ChaitanyaYeole02 commented 2 years ago

This is inisde my central_system.py uvicorn.run(central_system, host="0.0.0.0", port=9000, headers=headers, workers=10)

This is what is error it gives me

WARNING:  You must pass the application as an import string to enable 'reload' or 'workers'.
[run - 562] You must pass the application as an import string to enable 'reload' or 'workers'.

How would I be able to solve this?

villekr commented 2 years ago

Hi,

In that case you need to modify the structure a bit so that you can reference to central_system asgi compliant interface properly in uvicorn start function argument. For example like this:

central_system = CentralSystem()
central_system.include_router(v16_provisioning_router)
central_system.include_router(v201_provisioning_router)

if __name__ == "__main__":
    subprotocols = f"{Subprotocol.ocpp201}, {Subprotocol.ocpp16}"
    headers = [("Sec-WebSocket-Protocol", subprotocols)]
    uvicorn.run(
        "central_system:central_system",
        host="0.0.0.0",
        port=9000,
        headers=headers,
        workers=10,
    )
ChaitanyaYeole02 commented 2 years ago

Whenever I try to keep the workers=2.

central_system = CentralSystem()
central_system.include_router(v16_provisioning_router)
central_system.include_router(v201_provisioning_router)

if __name__ == "__main__":
    subprotocols = f"{Subprotocol.ocpp201}, {Subprotocol.ocpp16}"
    headers = [("Sec-WebSocket-Protocol", subprotocols)]
    uvicorn.run("central_system:central_system",
                host="0.0.0.0",
                port=9000,
                headers=headers, 
                workers=2)

I receive an error

INFO:     Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
[bind_socket - 585] Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO:     Started parent process [8224]
[startup - 53] Started parent process [8224]
(CentralSystem) Startup.
(CentralSystem) Startup.
INFO:     Started server process [15220]
[serve - 75] Started server process [15220]
INFO:     Waiting for application startup.
[startup - 47] Waiting for application startup.
INFO:     Started server process [4252]
[serve - 75] Started server process [4252]
INFO:     Waiting for application startup.
[startup - 47] Waiting for application startup.
INFO:     Application startup complete.
[startup - 61] Application startup complete.
INFO:     Application startup complete.
[startup - 61] Application startup complete.
Process SpawnProcess-2:
Traceback (most recent call last):
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\c***l\PycharmProjects\asgi_ws\venv\lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "C:\Users\c***l\PycharmProjects\asgi_ws\venv\lib\site-packages\uvicorn\server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "C:\Users\c***l\PycharmProjects\asgi_ws\venv\lib\site-packages\uvicorn\server.py", line 77, in serve
    await self.startup(sockets=sockets)
  File "C:\Users\c***l\PycharmProjects\asgi_ws\venv\lib\site-packages\uvicorn\server.py", line 117, in startup
    server = await loop.create_server(
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1484, in create_server
    server._start_serving()
  File "C:\Users\c***l\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 313, in _start_serving
    sock.listen(self._backlog)
OSError: [WinError 10022] An invalid argument was supplied
ChaitanyaYeole02 commented 2 years ago

If I keep my function like this

uvicorn.run("central_system:central_system", host="0.0.0.0", port=9000, headers=headers, reload=True, workers=2)

Then the output I am receiving is

INFO:     Will watch for changes in these directories: ['C:\\Users\\cyeol\\PycharmProjects\\asgi_ws\\examples\\central_system\\standalone']
[__init__ - 359] Will watch for changes in these directories: ['C:\\Users\\cyeol\\PycharmProjects\\asgi_ws\\examples\\central_system\\standalone']
INFO:     Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
[bind_socket - 585] Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO:     Started reloader process [4016] using WatchFiles
[startup - 72] Started reloader process [4016] using WatchFiles
(CentralSystem) Startup.
INFO:     Started server process [13404]
[serve - 75] Started server process [13404]
INFO:     Waiting for application startup.
[startup - 47] Waiting for application startup.
INFO:     Application startup complete.
[startup - 61] Application startup complete.

I am not getting any errors but I have a doubt that is it even running 2 workers at the same time.

villekr commented 2 years ago

First of all I tested with and without reload argument and it definitely works. If curious you can give central system a random id and display that e.g. on_startup to validate that uvicorn has actually spun up multiple processes.

About you error: OSError: [WinError 10022] An invalid argument was supplied. That must be something about your environment, I really can't help with.

ChaitanyaYeole02 commented 2 years ago

I am using Windows 11 and currently I am on PyCharms, Python 3.8