hmenyus / node-calls-python

Call Python from NodeJS directly in-process without spawning processes
MIT License
252 stars 26 forks source link

How to handle asyncio functions? #52

Closed FerLuisxd closed 1 year ago

FerLuisxd commented 1 year ago

Currently trying to call an async function

import asyncio
async def ask(question):
     return question

Jscode:

const result = await py.call(pymodule, "ask", "hello");
console.log(result)

Log:

sys:1: RuntimeWarning: coroutine 'ask' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
undefined
hmenyus commented 1 year ago

I am not sure what you want to do here. You want to call asynchronously a python function which is also an async function. py.call will run 'ask' on a separate thread, you do not need any async inside python.

Secondly async functions (corutines) in python cannot be called as regular functions, but py.call handles everything as a regular python function. You have to use asyncio.main(ask...) or call 'ask' from another async function and use await/wait_for, etc.