Giter VIP home page Giter VIP logo

Comments (2)

hoodmane avatar hoodmane commented on June 12, 2024

Thanks for the bug report @kor0p. It would definitely be helpful to improve the situation here. With handlers, the main question is whether the callee of the handler will want a sync context or an async one. I don't think there is any reasonable way to introspect this. It would be a good idea to add support for something like this in add_event_listener.

There are also more significant issues about the interaction between stack switching and and async contexts which will have to be ironed out. In particular, our event loop should ideally enter execution contexts with stack switching enabled if the runtime supports it. Since our event loop is calling into the function, we know the context is async. Though this could potentially lead to funny things like this not working:

def handler(event):
    function_that_uses_syncify()
    print('Hi here')

addListener(handler)

but being fixed if we make it into async def handler.

For now a possible workaround is:

from pyodide.ffi import create_proxy
import asyncio
import js

async def async_handler():
    await asyncio.sleep(0.1)
    print('Hi there')

def handler(event):
    asyncio.create_task(async_handler()).syncify()
    print('Hi here')

handler_px = create_proxy(handler)
f = handler_px.callSyncifying.bind(handler_px)
js.document.body.addEventListener('click', f)

from pyodide.

kor0p avatar kor0p commented on June 12, 2024

but being fixed if we make it into async def handler.

Great! I'll try it)

For now a possible workaround is:

from pyodide.ffi import create_proxy
import asyncio
import js

async def async_handler():
    await asyncio.sleep(0.1)
    print('Hi there')

def handler(event):
    asyncio.create_task(async_handler()).syncify()
    print('Hi here')

handler_px = create_proxy(handler)
f = handler_px.callSyncifying.bind(handler_px)
js.document.body.addEventListener('click', f)

Thanks! That also can help me)
But using async handlers as default looks like better choice 😅

(Off topic, just my emotions 🌚)
I'm very excited to be (for now just a little) part of Pyodide
It sounds crazy ðŸĪŠ, but I'm creating frontend framework based on Pyodide. Maybe I'm Pythonic to the core, but I believe that everything should be written in Python 🙃
So, if there is Python in browser, there should be Python frontend framework 😎
Thanks you all for your work here â™Ĩïļ

from pyodide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google âĪïļ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.