Skip to content

Using asyncio in _thread #8340

Open
Open

Description

@yjchun

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?)

Activity

  1. peterhinch commented on Feb 23, 2022

    @peterhinch
    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.

  2. added
    extmodRelates to extmod/ directory in source
    on Feb 25, 2022
  3. dpgeorge commented on Feb 25, 2022

    @dpgeorge
    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...).

  4. dpgeorge commented on Mar 29, 2022

    @dpgeorge
    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
  5. added a commit that references this issue on Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extmodRelates to extmod/ directory in source

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions