Skip to content

asyncio code cannot run simultaneously in multiple sub-interpreters. #96336

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

Closed
romanvm opened this issue Aug 27, 2022 · 3 comments
Closed

asyncio code cannot run simultaneously in multiple sub-interpreters. #96336

romanvm opened this issue Aug 27, 2022 · 3 comments
Labels
topic-asyncio topic-subinterpreters type-bug An unexpected behavior, bug, or error

Comments

@romanvm
Copy link

romanvm commented Aug 27, 2022

Bug report

asyncio code cannot run simultaneously in multiple sub-interpreters residing in their own threads. If you try to start an event loop by calling asyncio.run() only the first sub-interpreter will succeed and others will fail with the following error:

File "/usr/lib/python3.10/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

A minimal example to reproduce the issue:

#include <iostream>
#include <thread>

#include "Python.h"


const char* pyCode = R"""(
import asyncio
import threading

async def coro():
    await asyncio.sleep(3)
    print(f'Sleep in thread {threading.get_ident()} done')

asyncio.run(coro())
)""";


void runPython() {
  PyThreadState* ts = PyThreadState_New(PyInterpreterState_Main());
  PyEval_RestoreThread(ts);

  PyThreadState* interpTs = Py_NewInterpreter();

  PyRun_SimpleString(pyCode);
  PyObject* error = PyErr_Occurred();
  if (error) {
   PyErr_PrintEx(0);
  }

  Py_EndInterpreter(interpTs);

  PyThreadState_Swap(ts);
  PyEval_ReleaseThread(ts);
}


int main() {
  Py_Initialize();
  Py_BEGIN_ALLOW_THREADS

  std::thread thread1(runPython);
  std::thread thread2(runPython);
  thread1.join();
  thread2.join();

  Py_END_ALLOW_THREADS
  Py_Finalize();

  std::cout << "All done" << std::endl;

  return 0;
}

If you try to do the same without sub-interpeters everything works fine:

#include <iostream>
#include <thread>

#include "Python.h"


const char* pyCode = R"""(
import asyncio
import threading

async def coro():
    await asyncio.sleep(3)
    print(f'Sleep in thread {threading.get_ident()} done')

asyncio.run(coro())
)""";


void runPython() {
  PyGILState_STATE gstate = PyGILState_Ensure();

  PyRun_SimpleString(pyCode);
  PyObject* error = PyErr_Occurred();
  if (error) {
   PyErr_PrintEx(0);
  }
  
  PyGILState_Release(gstate);
}


int main() {
  Py_Initialize();
  Py_BEGIN_ALLOW_THREADS

  std::thread thread1(runPython);
  std::thread thread2(runPython);
  thread1.join();
  thread2.join();

  Py_END_ALLOW_THREADS
  Py_Finalize();

  std::cout << "All done" << std::endl;

  return 0;
}

Background: in Kodi project we use sub-interpreters to run Python add-ons in isolated (-ish) environments. But this issue prevents from using asyncio in multiple addons at the same time.

A clear and concise description of what the bug is.
Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible.

Your environment

  • CPython versions tested on: Python 3.10
  • Operating system and architecture: Linux Ubuntu x86-64.
@romanvm romanvm added the type-bug An unexpected behavior, bug, or error label Aug 27, 2022
@AlexWaygood
Copy link
Member

I believe this may be a duplicate of #91375 (@kumaraditya303, can you confirm? :)

@kumaraditya303
Copy link
Contributor

Yeah, it is a duplicate of #91375. The workaround I posted in that issue should be applicable here.

@AlexWaygood
Copy link
Member

Duplicate of #91375

@AlexWaygood AlexWaygood marked this as a duplicate of #91375 Aug 27, 2022
@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio topic-subinterpreters type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants