PythonMonkey icon indicating copy to clipboard operation
PythonMonkey copied to clipboard

PythonMonkey cannot find a running Python event-loop to make asynchronous calls.

Open arvindavoudi opened this issue 11 months ago • 1 comments

Issue type

Bug

How did you install PythonMonkey?

Installed from pip

OS platform and distribution

MacOS 15.1

Python version (python --version)

3.11.9

PythonMonkey version (pip show pythonmonkey)

1.1.0

Bug Description

Hi, I attempted to load the lightstreamer-client JavaScript library in Python using PythonMonkey as shown below:

import pythonmonkey as pm
lsc = pm.require("lightstreamer-client")

However, I encountered the following error:

Traceback (most recent call last):
  File ".../test.py", line 4, in <module>
    lsc = pm.require("lightstreamer-client")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/pythonmonkey/require.py", line 431, in require
    return createRequire(filename)(moduleIdentifier)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: PythonMonkey cannot find a running Python event-loop to make asynchronous calls.

Could you provide guidance on how to resolve this issue? Thank you!

arvindavoudi avatar Jan 22 '25 20:01 arvindavoudi

Hi @arvindavoudi , this happens when the JavaScript code requires an event loop (for example: code that uses setTimeout)

You need to evaluate the JS code in a Python event loop. Refer to the asyncio standard library: https://docs.python.org/3/library/asyncio.html

Not sure if this will work in your use case, but try running:

import asyncio
import pythonmonkey as pm

async def pm_async_wrapper():
  lsc = pm.require("lightstreamer-client")

asyncio.run(pm_async_wrapper())

wiwichips avatar Mar 25 '25 15:03 wiwichips