Skip to content
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

Using asyncio in _thread #8340

Open
yjchun opened this issue Feb 22, 2022 · 3 comments
Open

Using asyncio in _thread #8340

yjchun opened this issue Feb 22, 2022 · 3 comments
Labels

Comments

@yjchun
Copy link
Contributor

yjchun commented Feb 22, 2022

Hello.
This might be a feature request: asyncio is missing support for threading

import uasyncio as asyncio
import _thread
import time


async def test1(arg):
    try:
        while True:
            print(arg)
            await asyncio.sleep(1)

    except Exception as e:
        print('Exception in '+arg)
        print(e)


def run(arg):
    try:
        asyncio.run(test1(arg))
    except Exception as e:
        print('Exception in asyncio.run ' + arg)
        print(e)
    print("END "+arg)


_thread.start_new_thread(run, ('core1',))
time.sleep_ms(100)
run('core0')

Tested in two ports: unix with uasyncio module included and rp2 with #8310 patch
Exception within a few seconds in both ports.
Sometimes ValueError: generator already executing
Sometimes 'NoneType' object has no attribute 'ph_child'

Also I would like to create new event loop in new thread (which is automatically done in CPython). Do you think it is possible to implement proper event loop? or would there be limitations in VM? (is generator is thread safe in VM level?)

@peterhinch
Copy link
Contributor

You beat me to it. Give #8310 and apparently reliable two-core operation I was about to try this. I can see a definite use case for a copy of uasyncio running on each core.

@dpgeorge
Copy link
Member

Yes this would be a good enhancement.

(Related, I've sometimes wanted to run a separate, independent uasyncio instance within a soft IRQ handler...).

@dpgeorge
Copy link
Member

Some thoughts on this:

  • all global state in uasyncio (which is mostly in core.py) needs to be duplicated for each core/thread
  • then there needs to be a way for all uasyncio functions to know what state they are associated with
  • it would make sense to put all state in Loop, and then it might be possible to pass a loop argument around so that functions know the asyncio instance they are attached to. But this would require passing loop into everything, including calls like .read() and .write(), and that would break all existing asyncio based code
  • instead of passing loop around, it would need to be made implicit and the current loop instance instead stored in some thread-local-state
  • for running uasyncio instances within a soft interrupt handler, that code can explicitly set the loop instance on entry to the handler, then restore the original loop on exit

tannewt added a commit to tannewt/circuitpython that referenced this issue Aug 29, 2023
Use QSTR_GLOBAL_REQUIREMENTS to give explicit dependency for qstr gen…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants