filcuc / nimqml

Qt Qml bindings for the Nim programming language
Other
164 stars 20 forks source link

Async freezes in nimqml-application #21

Open R3D9477 opened 6 years ago

R3D9477 commented 6 years ago

Hi. I strange behavior of async/await operaions, when I called into nimqml-signal. ChatServer freezes at server.nim:38 chat_server_app.zip

Here is client for test: async_client_server.zip (also in this client can be tested server.nim without nimqml and it works fine)

Any ideas?

dom96 commented 6 years ago

I haven't looked at your code, but I'm guessing you haven't properly integrated into the nimqml event loop? You likely need to call some sort of QML poll function together with the asyncdispatch poll function.

R3D9477 commented 6 years ago

@dom96 well, what I trying to do: 1) to start server:

...
  proc btnStartClicked*(self: MainWindowHandler, serverPort: int) {.slot.} =
    self.chatServer.start(serverPort)
    serverStartedChanged(self, self.chatServer.started)
...

2) to call procedure 'start' after button clicked:

...
proc start*(server: Server, serverPort: int) =
  server.socket = newAsyncSocket()
  server.started = true
  server.socket.bindAddr(serverPort.Port)
  server.socket.listen()
  server.loopTask = loop(server)
...

3) then to launch async task 'loop', who will wait for clients:

...
proc loop(server: Server) {.async.} =
  echo 000000000
  while server.started:
    echo 111111111
    let (netAddr, clientSocket) = await server.socket.acceptAddr()
    echo 222222222
    ...
...

nothing more.

and as result output I have:

0
111111111

client tells me about successfully connection, but in that time server doesn't go to next line echo 222222222

filcuc commented 6 years ago

@r3d9u11 @dom96

Qt is not made (usually) for being called by other threads and you must be careful on doing that. Usually Qt should "own" the main thread for multiple reasons (initialization of static QtObjects in .so libraries that must be destroyed on app exit thus on the same thread). Honestly i don't have a lot of experience in integrating Qt with other event loops. The best approach is to separate your async server from the main thread and communicate with an "Actor" like pattern. In the worst case split the server and the UI in two different processes and communicate with a Pipe or ZMQ or nanomsg

R3D9477 commented 6 years ago

@filcuc thanks for hint, I'll try to play with threading. but I'll leave this issue to be open, yet

R3D9477 commented 6 years ago

@dom96 async procedures isn't GC safe? Is it possible to call async procedure from another procedure who called in a new thread via spawn?

dom96 commented 6 years ago

You can write {.async, gcsafe.}, no?

R3D9477 commented 6 years ago

@dom96 yes, thanks! (sorry I'm a novice in Nim, yet)

filcuc commented 6 years ago

@r3d9u11 @dom96 probably i've just to export the QApplication::processEvents() in the DOtherSide library

R3D9477 commented 6 years ago

@filcuc can it help to solve this situation?

filcuc commented 6 years ago

I will give it a look asap

filcuc commented 5 years ago

with https://github.com/filcuc/DOtherSide/commit/b6fcb521bfbf78607dd7d571518367f74c21277e in place i can proceed and fix this issue

R3D9477 commented 5 years ago

@filcuc great news! can I close this issue?

filcuc commented 5 years ago

No yet. I'll close this one when i add the bindings in NimQml. After that i would like to create a test project that integrates nim async with Qt event loop. Let's see if it's feasible.

Il giorno ven 4 ott 2019 alle ore 09:21 r3d9u11 notifications@github.com ha scritto:

@filcuc https://github.com/filcuc great news! can I close this issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/filcuc/nimqml/issues/21?email_source=notifications&email_token=ABWTXI5NKVVNAH5TC3PBSRLQM3VHLA5CNFSM4EVQ5WC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAKXMTQ#issuecomment-538277454, or mute the thread https://github.com/notifications/unsubscribe-auth/ABWTXIYVQRKIJQRQU6YJQOLQM3VHLANCNFSM4EVQ5WCQ .

-- Filippo Cucchetto