Skip to content

Strange interaction with multiple greenlets with the same parent and asyncio/coroutine/generator #173

Closed
@CaselIT

Description

@CaselIT

I was experimenting with the example provided in the documentation and I noticed a strange behavior when using asyncio.
This is an example script:

import asyncio
import greenlet

def syncg():
    print("before g2")
    print("g2 result", syncg2.switch())
    print("after g2")

def g2sync():
    print("in g2")
    return 42

syncg1 = greenlet.greenlet(syncg)
syncg2 = greenlet.greenlet(g2sync)
print("sync version")
print("sync result", syncg1.switch())

async def asyncg():
    print("before g2")
    print("g2 result", asyncg2.switch())
    print("after g2")

def g2async():
    print("in g2")
    return 42

asyncg1 = greenlet.greenlet(asyncg)
asyncg2 = greenlet.greenlet(g2async)
print("asyncio version")
print("asyncio result", asyncio.run(asyncg1.switch()))

My understanding is that these two cases should be equal and print the lines "before g2" and "in g2" and return 42.
The async version resumes the execution of asyncg1 after asyncg2 exits instead.
This is the output:

sync version
before g2
in g2
sync result 42
asyncio version
before g2
in g2
g2 result 42
after g2
asyncio result None

python: 3.8.3
greenlet: 0.4.16

I tested on windows but the same behavior happen on linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions