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

asyncio support #373

Closed
pohmelie opened this issue May 2, 2017 · 4 comments · Fixed by #408
Closed

asyncio support #373

pohmelie opened this issue May 2, 2017 · 4 comments · Fixed by #408

Comments

@pohmelie
Copy link

pohmelie commented May 2, 2017

It looks like there is cheap way to use asyncio with flexx. But I'm not sure how it will work with many connections from browser, since I'm not familiar with flexx internals. More than one event loop? Threads?

import asyncio

from flexx import app, ui

import tornado.ioloop


tornado.ioloop.IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')


@app.serve
class Example(ui.Widget):

    def init(self):
        with ui.HBox():
            ui.Button(flex=1, text="hello")
            ui.Button(flex=1, text="world")

        asyncio.ensure_future(self.yoba())

    async def yoba(self):
        while True:
            loop = asyncio.get_event_loop()
            print(id(loop), loop)
            await asyncio.sleep(1)


app.start()

I'm asking core devs to inspect this code and check corner cases. It will be very nice, if flexx will support asyncio officially and documentation takes paragraph about it.

@pohmelie
Copy link
Author

pohmelie commented May 3, 2017

Hm, it looks like this does not work with current master. But works fine with pypi version.
Update:
It works only with python 3.6 (both master and pypi versions).

@pohmelie
Copy link
Author

pohmelie commented May 3, 2017

Ok, more canonical example from documentation works fine on python 3.4.6 with current master.
http://www.tornadoweb.org/en/stable/asyncio.html#tornado.platform.asyncio.AsyncIOMainLoop

import asyncio

from flexx import app, ui

from tornado.platform.asyncio import AsyncIOMainLoop


class Example(ui.Widget):

    def init(self):
        with ui.HBox():
            ui.Button(flex=1, text="hello")
            ui.Button(flex=1, text="world")

        asyncio.ensure_future(self.yoba())

    @asyncio.coroutine
    def yoba(self):
        while True:
            loop = asyncio.get_event_loop()
            print(id(loop), loop)
            yield from asyncio.sleep(1)


AsyncIOMainLoop().install()
app.serve(Example)
app.start()

Update:
More to say, flexx import should be before tornado import 👀 … strange magic.

@almarklein
Copy link
Member

But I'm not sure how it will work with many connections from browser, since I'm not familiar with flexx internals. More than one event loop? Threads?

Multiple connections are simply handled as events. There is exactly one event loop (in a single thread).

Leaving this open as a todo to document the usage. Also related to #142

@almarklein
Copy link
Member

#408 implements Flexx' event loop on top of asyncio, also see #413 to take this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants