Nuitka icon indicating copy to clipboard operation
Nuitka copied to clipboard

Nuitka8.1 with Sanic,RuntimeWarning: coroutine 'BaseEventLoop.create_server' was never awaited

Open HB4daemmon opened this issue 2 years ago • 6 comments

I freeze a simple sanic project with nuitka on windows11, but it doesn't work correctly。

  • Env: Windows11、Python3.7(64bit)、Nuitka8.1
  • Nuitka-Options:INFO: Used command line options: --follow-imports --standalone --mingw64 server.py
  • server.exe result

[2022-05-25 13:07:22 +0800] [11256] [ERROR] Experienced exception while trying to serve Traceback (most recent call last): File "D:\Projects\tavi\server.dist\sanic\mixins\runner.py", line 578, in serve File "D:\Projects\tavi\server.dist\sanic\server\runners.py", line 206, in serve_single File "D:\Projects\tavi\server.dist\sanic\server\runners.py", line 129, in serve File "D:\Projects\tavi\server.dist\asyncio\base_events.py", line 579, in run_until_complete File "D:\Projects\tavi\server.dist\sanic\app.py", line 1560, in _startup File "D:\Projects\tavi\server.dist\sanic\touchup\service.py", line 26, in run File "D:\Projects\tavi\server.dist\sanic\touchup\schemes\base.py", line 20, in call File "D:\Projects\tavi\server.dist\sanic\touchup\schemes\ode.py", line 24, in run File "D:\Projects\tavi\server.dist\inspect.py", line 973, in getsource File "D:\Projects\tavi\server.dist\inspect.py", line 955, in getsourcelines File "D:\Projects\tavi\server.dist\inspect.py", line 786, in findsource OSError: could not get source code Traceback (most recent call last): File "D:\Projects\tavi\server.dist\server.py", line 26, in File "D:\Projects\tavi\server.dist\sanic\mixins\runner.py", line 145, in run File "D:\Projects\tavi\server.dist\sanic\mixins\runner.py", line 578, in serve File "D:\Projects\tavi\server.dist\sanic\server\runners.py", line 206, in serve_single File "D:\Projects\tavi\server.dist\sanic\server\runners.py", line 129, in serve File "D:\Projects\tavi\server.dist\asyncio\base_events.py", line 579, in run_until_complete File "D:\Projects\tavi\server.dist\sanic\app.py", line 1560, in _startup File "D:\Projects\tavi\server.dist\sanic\touchup\service.py", line 26, in run File "D:\Projects\tavi\server.dist\sanic\touchup\schemes\base.py", line 20, in call File "D:\Projects\tavi\server.dist\sanic\touchup\schemes\ode.py", line 24, in run File "D:\Projects\tavi\server.dist\inspect.py", line 973, in getsource File "D:\Projects\tavi\server.dist\inspect.py", line 955, in getsourcelines File "D:\Projects\tavi\server.dist\inspect.py", line 786, in findsource OSError: could not get source code sys:1: RuntimeWarning: coroutine 'BaseEventLoop.create_server' was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback

  • server.py
import asyncio
from multiprocessing import Manager,freeze_support
from sanic import Sanic
from sanic.response import text

freeze_support()

app = Sanic("MyHelloWorldApp")

@app.get("/")
async def hello_world(request):
    return text("Hello, world.")

async def task1(app):
    while True:
        await asyncio.sleep(1)
        print("Hello, world.")

@app.listener("main_process_start")
async def start(app,loop):
    print("main_process_start")
    app.ctx.manager = Manager().dict()

app.add_task(task1(app))
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=1337)
  • pip freeze

aiofiles==0.8.0 altgraph==0.17.2 future==0.18.2 httptools==0.4.0 importlib-metadata==4.11.4 multidict==6.0.2 Nuitka==0.8.1 ordered-set==4.1.0 pefile==2021.9.3 pyinstaller==5.1 pyinstaller-hooks-contrib==2022.5 pywin32-ctypes==0.2.0 sanic==22.3.2 sanic-routing==22.3.0 typing-extensions==4.2.0 websockets==10.3 zipp==3.8.0

HB4daemmon avatar May 25 '22 05:05 HB4daemmon

You didn't see this, did you OSError: could not get source code. It seems it wants to check source code for whatever reason, any idea why?

kayhayen avatar May 25 '22 06:05 kayhayen

You didn't see this, did you OSError: could not get source code. It seems it wants to check source code for whatever reason, any idea why?

Yes, you are right. I checked Sanic souce code and found "touchup" modules load source code. I changed some configuration and now it works. Thanks for checking this.

app.config.TOUCHUP = False

HB4daemmon avatar May 25 '22 08:05 HB4daemmon

What is this touchup for?

kayhayen avatar May 25 '22 15:05 kayhayen

Over @ Linux (ubuntu 22.04 lts), I'm getting a different error:

FileNotFoundError: [Errno 2] No such file or directory: 'main.dist/sanic_ext/extensions/openapi/ui/redoc.html'

If I copy in the stuff requires from site-packages (basically the whole sanic_ext package) manually I get the same error as above.

Pirulax avatar Jul 28 '22 20:07 Pirulax

OP's error

Adding the following argument when running Nuitka should help:

--include-data-dir=".venv/lib/python3.10/site-packages/sanic=sanic"

The actual path to your sanic installation may very, in my case I have it inside my venv.

My issue (above)

FileNotFoundError: [Errno 2] No such file or directory: 'main.dist/sanic_ext/extensions/openapi/ui/redoc.html'

This should only happen if you have the sanic_ext package installed too. It happens because Sanic tries generating the openapi docs. It can be solved in 2 ways:

  • Either copy the whole sanic_ext package into the output folder (the one ending with .dist, eg.: main.dist)
  • Or just disable it altogether:
app = Sanic("ur-app")
app.extend(config={"oas": False})

Thanks for the help from the guys over at the Sanic Discord, especially to Adam Hopkins.

Pirulax avatar Jul 28 '22 21:07 Pirulax

Manual copying is not supported at all. Rather than using paths, using --include-package-data would make more sense. If somebody figures out, which files to add, please add them to the Nuitka package configuration file as Yaml.

kayhayen avatar Jul 30 '22 15:07 kayhayen

@Pirulax if that's a default, anti-bloat and the Yaml configuration could make that change automatically, because I doubt people want that to happen

kayhayen avatar Mar 12 '23 17:03 kayhayen

This will make it straight to 2.0.2 too, once released, hopefully soon.

kayhayen avatar Feb 09 '24 11:02 kayhayen

Great, thank you very much for the effort!

Pirulax avatar Feb 09 '24 16:02 Pirulax

OP's error

Adding the following argument when running Nuitka should help:

--include-data-dir=".venv/lib/python3.10/site-packages/sanic=sanic"

The actual path to your sanic installation may very, in my case I have it inside my venv.

My issue (above)

FileNotFoundError: [Errno 2] No such file or directory: 'main.dist/sanic_ext/extensions/openapi/ui/redoc.html'

This should only happen if you have the sanic_ext package installed too. It happens because Sanic tries generating the openapi docs. It can be solved in 2 ways:

  • Either copy the whole sanic_ext package into the output folder (the one ending with .dist, eg.: main.dist)
  • Or just disable it altogether:
app = Sanic("ur-app")
app.extend(config={"oas": False})

Thanks for the help from the guys over at the Sanic Discord, especially to Adam Hopkins.

hadn't noticed this comment, in order to reproduce, is all that's needed is for the sanic_ext package to be available? or do i have to take other steps to reproduce?

KRRT7 avatar Feb 09 '24 16:02 KRRT7

I don't remember it anymore sadly. but iirc you just have to install sanic_ext and that's it (and perhaps import it in the py code)

Pirulax avatar Feb 09 '24 18:02 Pirulax

Copying anything was never supported or supposed to work, so lets not consider that at all. For the generation of docs, that probably needs to be disabled, if that's the case that it does this on the side, typical anti-bloat. So was I closing this prematurely or does it work for you @KevinRodriguez777 ?

kayhayen avatar Feb 10 '24 15:02 kayhayen

For the generation of docs, that probably needs to be disabled

i don't think so, if it does something like FastAPI openAPI swagger docs generation, then it probably shouldn't be disabled as it's an feature

So was I closing this prematurely or does it work for you @KevinRodriguez777 ?

no i think you're fine, the sanic_ext is basically a different package and i'll include the datafile later on

KRRT7 avatar Feb 11 '24 06:02 KRRT7

something like FastAPI openAPI swagger docs generation

[2024-02-11 02:01:36 -0500] [16896] [INFO] > openapi [http://0.0.0.0:8000/docs] yup. that's exactly what's happening, after looking at the HTML file itself, it's a template that gets edited with the generated HTML that it needs to serve the OpenAPI docs.

KRRT7 avatar Feb 11 '24 07:02 KRRT7

Ok, I will not look deeply into these things, I trust you will handle it.

kayhayen avatar Feb 11 '24 11:02 kayhayen