# I try to use asyncio rather than qt.QTcpSocket as the client, but the socket can receive msgs only if the mouse is hovering the the Slicer UI

**URL:** <https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411>\
**Category:** Support\
**Tags:** python\
**Created:** [March 16, 2023, 7:43am UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411 "2023-03-16T07:43:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)\
**Post date:** [March 16, 2023, 7:43am UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411/1 "2023-03-16T07:43:22Z")

</div>

```auto
import asyncio

async def tcp_echo_client(message):
    reader, writer = await asyncio.open_connection(
        '127.0.0.1', 8888)

    print(f'Send: {message!r}')
    writer.write(message.encode())
    await writer.drain()

    data = await reader.read(100)
    print(f'Received: {data.decode()!r}')

    print('Close the connection')
    writer.close()
    await writer.wait_closed()

asyncio.run(tcp_echo_client('Hello World!'))

```

But this seems not be able to automatically get the messages from the server side.

```auto
loop = qt.QEventLoop()
asyncio.set_event_loop(loop)

```

Can I use the above to get qt eventloop to get the messages from the server automatically? Thanks!

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [March 16, 2023, 1:21pm UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411/2 "2023-03-16T13:21:36Z")

</div>

I haven’t seen a general purpose way to use our Qt event loop with asyncio, so if you or anyone here has a good solution please post it.

The [WebServer](https://slicer.readthedocs.io/en/latest/user_guide/modules/webserver.html) module in Slicer [uses](https://github.com/Slicer/Slicer/blob/main/Modules/Scripted/WebServer/WebServer.py#L492) the [QSocketNotifier](https://doc.qt.io/qt-5/qsocketnotifier.html) class to add a python socket’s file number to the Qt event loop so that everything is event driven. I find this to be a good solution.

There should be a way to do something similar with asyncio such that events from Qt can be used to trigger event handling in asyncio code, but when I looked a few years ago it seemed that the core event loops in asyncio were not set up for this use case. Maybe there are good solutions by now.

---

<div class="post-metadata">

**Author:** ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)\
**Post date:** [March 16, 2023, 1:27pm UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411/3 "2023-03-16T13:27:55Z")

</div>

Thanks a lot! Indeed helpful! Will try QSocketNotifier.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 16, 2023, 1:45pm UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411/4 "2023-03-16T13:45:07Z")

</div>

You may consider using the WebServer module in Slicer. Commonly needed features are exposed via the `slicer` endpoint, it has an `exec` option to allow full access to the entire Slicer Python API, and you can also start a SlicerHTTPServer with your own `RequestHandler` to support any custom requests. So, you actually don’t need to go down to the asincio or QSocketNotifier level or deal with any communication stuff at all, just implement a simple Python class that has `canHandleRequest` and `handleRequest` methods ([Slicer’s default request handler](https://github.com/Slicer/Slicer/blob/main/Modules/Scripted/WebServer/WebServerLib/SlicerRequestHandler.py) can serve as an example).

---

<div class="post-metadata">

**Author:** ![derekcbr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/derekcbr/32/18793_2.png) [@derekcbr](https://discourse.slicer.org/u/derekcbr)\
**Post date:** [March 17, 2023, 1:02am UTC](https://discourse.slicer.org/t/i-try-to-use-asyncio-rather-than-qt-qtcpsocket-as-the-client-but-the-socket-can-receive-msgs-only-if-the-mouse-is-hovering-the-the-slicer-ui/28411/5 "2023-03-17T01:02:13Z")

</div>

Thank you very much for the help! That is really great! Will try with it!
