Python 3.5/3.6 asyncio with wxPython Phoenix

I'm playing with MicroPython using asyncio coroutines to do some tasks
(e.g. scan a 4x4 keypad matrix and display stuff on a LCD).

I want to also be able to simulate my environment using wxPython Phoenix
to provide fake LCD and Keypad.

How can I use wxPython Phoenix with Python 3.5/3.7 asyncio
functionality, so that I can run the coroutines and also hand GUI events?

I've searched high and low for info but no hits.

Would it be something as simple as the following? I'm thinking there
must be some more magic involved.

    ## wx application
    wxapp = MyWxApp(False)

    ## Get a handle to the asyncio event loop.
    loop = asyncio.get_event_loop()

    ## Add coroutines to the event loop.
    loop.create_task( keypad_scanner_coro() )
    loop.create_task( keypad_watcher_coro() )
    loop.create_task( wxapp.MainLoop() )

    ## Start running the coroutines
    loop.run_forever()

Thanks, Brendan.

I'm playing with MicroPython using asyncio coroutines to do some tasks
(e.g. scan a 4x4 keypad matrix and display stuff on a LCD).

I also tried my PyBoard this week for the first time...

I want to also be able to simulate my environment using wxPython Phoenix
to provide fake LCD and Keypad.

How can I use wxPython Phoenix with Python 3.5/3.7 asyncio
functionality, so that I can run the coroutines and also hand GUI events?

Your example would not work, as MainLoop() does only return once you close your wx application.
As of now, there's no asyncio / wx integration that I'm aware of.
The MainLoop is implemented in C++, so it's probably not trivial to add support.

I have been using Qamash for some small programs (i.e. the PyQt/asyncio integration). asyncio support would probably be the coolest and most important feature to add to wxPython, once Phoenix is released.

As workaround, you could use a separate thread for the asyncio main loop, but that's not as nice as native support.
Alternatively, you may try to run the asyncio loop from within an EVT_IDLE handler. If you try this, please post your results.
Instead of calling loop.run_forever you would need to call _run_once on each idle event. See the implementation of run_forever.

Regards,

Dietmar

···

On 12.03.2017 08:02, Brendan Simon (eTRIX) wrote:

    > functionality, so that I can run

the coroutines and also hand GUI events?

  > How can I use wxPython Phoenix with Python 3.5/3.7 asyncio


  Your example would not work, as MainLoop() does only return once

you

  close your wx application.


  As of now, there's no asyncio / wx integration that I'm aware of.


  The MainLoop is implemented in C++, so it's probably not trivial

to add

  support.




  I have been using Qamash for some small programs (i.e. the

PyQt/asyncio

  integration). asyncio support would probably be the coolest and

most

  important feature to add to wxPython, once Phoenix is released.




  As workaround, you could use a separate thread for the asyncio

main

  loop, but that's not as nice as native support.


  Alternatively, you may try to run the asyncio loop from within an


  EVT_IDLE handler. If you try this, please post your results.


  Instead of calling loop.run_forever you would need to call

_run_once on

  each idle event. See the implementation of run_forever.




  Dietmar
Thanks Dietmar,

  OK.  I think I'll try the EVT_IDLE approach to start with and see

how I go.

  I figured there must have been some kind of async integration as

Twisted has some kind of method to use wxPython.

  Can something similar be done using asyncio?

Brendan.

···

http://twistedmatrix.com/documents/12.3.0/core/howto/choosing-reactor.html#auto13


    **eTRIX pty ltd**

    PO Box 497, Inverloch, VIC 3996, AUSTRALIA.

    +61-(0)417-380-984

accounts@etrix.com.au

Here is an example I put together.

  It has 4 gauges in a dialog.  Gauge 1 is updated via wxTimer every

100ms. Gauges 2-4 are updated via asyncio coroutines/tasks, every
200ms, 300ms, 400ms respectively.
What is interesting is that gauges are not as synchronised as I
would expect. i.e. gauge 1 and 2 should end exactly the same
time, every 200ms, and gauge 4 should also end at the same time as
gauge 1 and 2 every 400ms, but they are out. It might be my count
reset logic is out by 1?
Brendan.

···

https://github.com/BrendanSimon/micropython_experiments/blob/master/keypad_lcd/wx_asyncio_test_1.py


    **eTRIX pty ltd**

    PO Box 497, Inverloch, VIC 3996, AUSTRALIA.

    +61-(0)417-380-984

accounts@etrix.com.au

never tried with asyncio but maybe you can get hints from this wxpython+gevent example

···

On Sunday, March 12, 2017 at 8:02:45 AM UTC+1, Brendan Simon wrote:

I’m playing with MicroPython using asyncio coroutines to do some tasks

(e.g. scan a 4x4 keypad matrix and display stuff on a LCD).

I want to also be able to simulate my environment using wxPython Phoenix

to provide fake LCD and Keypad.

How can I use wxPython Phoenix with Python 3.5/3.7 asyncio

functionality, so that I can run the coroutines and also hand GUI events?

I’ve searched high and low for info but no hits.

Would it be something as simple as the following? I’m thinking there

must be some more magic involved.

## wx application

wxapp = MyWxApp(False)



## Get a handle to the asyncio event loop.

loop = asyncio.get_event_loop()



## Add coroutines to the event loop.

loop.create_task( keypad_scanner_coro() )

loop.create_task( keypad_watcher_coro() )

loop.create_task( wxapp.MainLoop() )



## Start running the coroutines

loop.run_forever()

Thanks, Brendan.

Possibly try https://github.com/sirk390/wxasync

wxasync it a library for using python 3 asyncio (async/wait) with wxpython. It does GUI message polling every 5ms and runs the asyncio message loop the rest of the time.