[PyQt] Integrating asyncio with Qt event loop?

Erik Hvatum ice.rikh at gmail.com
Thu Aug 7 23:13:54 BST 2014


Brilliant!  Works like a charm; our code
<https://github.com/erikhvatum/zplab/blob/68328cb37289bdf94ae0b10d4dc5e9583578934d/acquisition/dm6000b/function_units/objective_turret.py>
is going to get a lot cleaner.

Tusen takk,
Erik


On Thu, Aug 7, 2014 at 4:29 PM, Arve Knudsen <arve.knudsen at gmail.com> wrote:

> Hi Erik
>
> I think you could use an asyncio.Future for this purpose. You can yield
> from the Future, and set its result upon arrival of the signal. I've
> concocted an example program that should show what I mean; it creates a
> 2-second timer which is waited on asynchronously, once its timeout fires
> the program exits:
>
>     import quamash
>     import asyncio
>     from PyQt5.QtWidgets import *
>     from PyQt5.QtCore import *
>
>
>     @asyncio.coroutine
>     def _go():
>         def on_timeout():
>             print('Timeout')
>             fut.set_result(True)
>
>         fut = asyncio.Future()
>         timer = QTimer()
>         timer.setSingleShot(True)
>         timer.setInterval(2000)
>         timer.start()
>         timer.timeout.connect(on_timeout)
>         print('Yielding until signal...')
>         yield from fut
>         print('Continuing execution after yield from')
>
>     with quamash.QEventLoop(app=QApplication([])) as loop:
>         w = QMainWindow()
>         w.show()
>         loop.run_until_complete(_go())
>     print('Coroutine has ended')
>
> HTH,
> Arve
>
>
> On Thu, Aug 7, 2014 at 12:31 AM, Erik Hvatum <ice.rikh at gmail.com> wrote:
>
>> Thank you :)
>>
>> A quick question to help as I dig into this: how might one go about
>> "yield from wait_for_a_qt_signal"?  That is, I want my coroutine to resume
>> execution when some certain signal is emitted.  Perhaps this desire
>> indicates that I'm going about things the wrong way...
>>
>> -Erik
>>
>> PS: forgot to reply to the list the first time around
>>
>>
>> On Sat, Jul 5, 2014 at 12:34 PM, Arve Knudsen <arve.knudsen at gmail.com>
>> wrote:
>>
>>> Quamash, in the unix branch (
>>> https://github.com/aknuds1/quamash/tree/unix), now works perfectly for
>>> thread and subprocess execution on OS X, Linux and Windows, I'm pleased to
>>> say. Would love some feedback.
>>>
>>> Arve
>>>
>>>
>>> On Tue, Jul 1, 2014 at 3:12 PM, Arve Knudsen <arve.knudsen at gmail.com>
>>> wrote:
>>>
>>>> Now, even subprocess execution works. At least on Windows/PyQt, haven't
>>>> tested on other platforms.
>>>>
>>>> Arve
>>>>
>>>>
>>>> On Mon, Jun 30, 2014 at 4:03 PM, Arve Knudsen <arve.knudsen at gmail.com>
>>>> wrote:
>>>>
>>>>> Erik, Tamás, feel free to try my Quamash fork
>>>>> <https://github.com/aknuds1/quamash>, it's currently quite
>>>>> functional, except that subprocess execution is somehow broken.
>>>>>
>>>>> Arve
>>>>>
>>>>> On Fri, Jun 27, 2014 at 1:34 PM, Arve Knudsen <arve.knudsen at gmail.com>
>>>>> wrote:
>>>>>
>>>>>> My fork is now semi-functional with Python 3.4 and PyQt 5. I can at
>>>>>> least perform simple asynchronous tasks within a Qt application, but lots
>>>>>> remain to implement.
>>>>>>
>>>>>> Arve
>>>>>>
>>>>>>
>>>>>> On Fri, Jun 27, 2014 at 9:24 AM, Arve Knudsen <arve.knudsen at gmail.com
>>>>>> > wrote:
>>>>>>
>>>>>>> I've created a fork on GitHub: https://github.com/aknuds1/quamash.
>>>>>>> Do you know where to locate information on writing an asyncio event loop
>>>>>>> though? Not sure where to begin.
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Arve
>>>>>>>
>>>>>>>
>>>>>>> On Thu, Jun 26, 2014 at 9:30 PM, Arve Knudsen <
>>>>>>> arve.knudsen at gmail.com> wrote:
>>>>>>>
>>>>>>>> Thanks. I'm thinking I might try to modify his code, if it isn't
>>>>>>>> too much work.
>>>>>>>>
>>>>>>>> Arve
>>>>>>>>
>>>>>>>>
>>>>>>>> On Thu, Jun 26, 2014 at 7:32 PM, Tamás Bajusz <gbtami at gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Unfortunately, I know that github project only. (
>>>>>>>>> https://github.com/harvimt/quamash)
>>>>>>>>> Try to contact him at mark.harviston at gmail.com
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Thu, Jun 26, 2014 at 7:27 PM, Arve Knudsen <
>>>>>>>>> arve.knudsen at gmail.com> wrote:
>>>>>>>>>
>>>>>>>>>> Tamás, has Mark Harviston's implementation been updated to work
>>>>>>>>>> with the latest Python (3.4)? I think I read somewhere that it's based on
>>>>>>>>>> an outdated API.
>>>>>>>>>>
>>>>>>>>>> Arve
>>>>>>>>>>
>>>>>>>>>> On Thu, Jun 26, 2014 at 6:29 PM, Tamás Bajusz <gbtami at gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> http://haypo-notes.readthedocs.org/asyncio.html#replacing-the-event-loop
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> On Thu, Jun 26, 2014 at 5:20 PM, Arve Knudsen <
>>>>>>>>>>> arve.knudsen at gmail.com> wrote:
>>>>>>>>>>>
>>>>>>>>>>>>  Hi guys
>>>>>>>>>>>>
>>>>>>>>>>>> I am looking to integrate Python 3.4 asyncio
>>>>>>>>>>>> <https://docs.python.org/3/library/asyncio.html> with Qt's
>>>>>>>>>>>> event loop (via PyQt 5), specifically in order to asynchronously monitor
>>>>>>>>>>>> output (stdout/stderr) from a child process. Maybe this is somewhat
>>>>>>>>>>>> off-topic for this list, but can someone tell me if such integration exists
>>>>>>>>>>>> (and works)?
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks,
>>>>>>>>>>>> Arve
>>>>>>>>>>>>
>>>>>>>>>>>> _______________________________________________
>>>>>>>>>>>> PyQt mailing list    PyQt at riverbankcomputing.com
>>>>>>>>>>>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140807/b2482aeb/attachment.html>


More information about the PyQt mailing list