Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Exchanging data between a PyQt5 app asynchronously
QtWS25 Last Chance

Exchanging data between a PyQt5 app asynchronously

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 227 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    batton
    wrote on last edited by
    #1

    I have a need to create a PyQt5 app that responds to asynchronous messages. I have a langgraph LLM application and I would like to pass data that it generates to a PyQt5 app to display the data, allow changes, and then pass data back to the Langgraph app.
    I know how to send the message from langgraph asynchronously using asyncio queues, but I'm an absolute beginner with PyQt.

    I think the way this works is like this:

    1. Langgraph has an asynchronous python function that puts the data to an async queue using the asyncio.Queue put() method.
    2. somewhere running in the PyQt app is aa async function that periodically executes and peforms asyncio.Queue get() method to read the queue.
    3. PyQt app displays the data which after modification is sent back to the Langgraph function by another call to an asyncio.Queue put() method.
    4. Langgraph function reads the queue once again with asyncio.Queue get() method and carries on.

    Can anybody please give me some pointers regarding if this is possible, and how I would do it in PyQt. It's only the PyQt part that is problematic for me. I don't need specific code examples (of course if snippets help, feel free), but rather a description of what components to use if it is possible.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I think you will be interested by the new Qt Asyncio module.

      It integrate the asyncio loop and Qt's event loop.

      For the rest what would you need to know ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        batton
        wrote on last edited by batton
        #3

        Thank you! Clearly I have a lot to learn. I was really wanting to do something very simple if possible.
        Below is a proof of concept using two functions, and how I would exchange a message between them:

        import asyncio
        
        
        async def function_1(sendQueue, receiveQueue):
            # Send a message to Function_2
            await sendQueue.put("Hello Function_2")
            print("Function_1: Sent 'Hello Function_2'")
        
            # Wait to receive a message from Function_2
            message = await receiveQueue.get()
            print(f"Function_1: Received '{message}'")
        
        
        async def function_2(sendQueue, receiveQueue):
            # Wait to receive a message from Function_1
            message = await sendQueue.get()
            print(f"Function_2: Received '{message}'")
        
            # Wait for 2 seconds
            await asyncio.sleep(2)
        
            # Send a message to Function_1
            await receiveQueue.put("Hello Function_1")
            print("Function_2: Sent 'Hello Function_1'")
        
        
        async def main():
            # Create the queues
            sendQueue = asyncio.Queue()
            receiveQueue = asyncio.Queue()
        
            # Run both functions concurrently
            await asyncio.gather(
                function_1(sendQueue, receiveQueue),
                function_2(sendQueue, receiveQueue)
            )
        

        If you were to replace Function_1 with my langgraph node function, and Function_2 as something in the PyQt app that would be triggered on a button press for example, then you would have an idea of what I am trying to achieve.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then I think the two examples linked in the doc I posted above show how to implement that kind of things.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            batton
            wrote on last edited by
            #5

            Thank you once again! I will dig into those examples.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved