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. Equivalent to asyncio.sleep?
Forum Updated to NodeBB v4.3 + New Features

Equivalent to asyncio.sleep?

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 677 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.
  • S Offline
    S Offline
    Stepan K.
    wrote on last edited by
    #1

    Hi,

    please is there some way to give up execution of a function for some time, like asyncio.sleep() - i.e. let Qt evaluate the event loop for a while and then return control back to the function?
    Example:
    have sequence of commands and e.g. one second pauses between them.
    CMD_1>sleep>CMD_2>sleep>CMD_3
    Either time.sleep() can be used, which would block the UI, or each CMD would set a singleshot timer to start the next command (which is ugly, hard do read and maintain, results in set of independent functions, requires an object to pass variables instead of simple local vars, but would do).
    In asyncio, there is this asyncio.sleep() that does exactly what I mean, returns control to the event loop for a while. I achieve this by series of time.sleep's calling event loop eval manually at this moment, to make the UI less blocked, but this is still rather ugly. Sure, a thread is another way, but again really ugly and resource heavy option.

    Thank You in advance,
    Stepan

    JonBJ 1 Reply Last reply
    0
    • S Stepan K.

      Hi,

      please is there some way to give up execution of a function for some time, like asyncio.sleep() - i.e. let Qt evaluate the event loop for a while and then return control back to the function?
      Example:
      have sequence of commands and e.g. one second pauses between them.
      CMD_1>sleep>CMD_2>sleep>CMD_3
      Either time.sleep() can be used, which would block the UI, or each CMD would set a singleshot timer to start the next command (which is ugly, hard do read and maintain, results in set of independent functions, requires an object to pass variables instead of simple local vars, but would do).
      In asyncio, there is this asyncio.sleep() that does exactly what I mean, returns control to the event loop for a while. I achieve this by series of time.sleep's calling event loop eval manually at this moment, to make the UI less blocked, but this is still rather ugly. Sure, a thread is another way, but again really ugly and resource heavy option.

      Thank You in advance,
      Stepan

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Stepan-K
      Given that you don't want to use approach like singleshot timer. I don't know if there is better equivalent to asyncio.sleep() (not my area).

      There are two obvious way of writing a blocking "sleep" inside a function which still allows the Qt event loop to run and then resumes at the next statement in the function:

      • Call Q[Core]Application::processEvents() in a loop, exiting after a period of time. If you call it many times without delay it is quite "busy". And in general calling processEvents() is frowned upon.

      • Use a local QEventLoop::exec() primed with a singleshot timer signal connected to QEventLoop::quit(). I think this is preferable to processEvents() in your own loop.

      These do not block like a sleep() would. Of course you must be aware that any arriving signals/events will be processed from within your call before returning to the next statement. Which may or may not be OK.

      Oh and btw, a different approach is to run your "command pipeline" in a secondary thread. Then you can use sleep() there. But you have to be careful about shared variable access and you must not access any UI stuff from any secondary thread.

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Stepan-K
        Given that you don't want to use approach like singleshot timer. I don't know if there is better equivalent to asyncio.sleep() (not my area).

        There are two obvious way of writing a blocking "sleep" inside a function which still allows the Qt event loop to run and then resumes at the next statement in the function:

        • Call Q[Core]Application::processEvents() in a loop, exiting after a period of time. If you call it many times without delay it is quite "busy". And in general calling processEvents() is frowned upon.

        • Use a local QEventLoop::exec() primed with a singleshot timer signal connected to QEventLoop::quit(). I think this is preferable to processEvents() in your own loop.

        These do not block like a sleep() would. Of course you must be aware that any arriving signals/events will be processed from within your call before returning to the next statement. Which may or may not be OK.

        Oh and btw, a different approach is to run your "command pipeline" in a secondary thread. Then you can use sleep() there. But you have to be careful about shared variable access and you must not access any UI stuff from any secondary thread.

        S Offline
        S Offline
        Stepan K.
        wrote on last edited by
        #3

        @JonB exactly - and thank You!
        You didn't find any better solution than I did I can see - a thread, singleshot timer or blocking shot time.sleep() with processEvents() repeated calling.

        Btw, sleep methods of the Qthread class are blocking I have found, so there likely isn't any difference in using these to an ordinary time.sleep().. also precision is rather limited (about 1/100sec? in Windows minimal - likely related to CPU context switchitng timer, whereas qtimer appears to support much higher precision, which is often important, too -- that may rule pausing done with time.sleep and processEvents() out for some situations and make qtimer and individual function calls for each of steps the only viable option).

        Well, maybe there really isn't any nicer way to achieve this type of serial processing in Qt, which is kind of shame - again, this is not a roadblock, just makes the code less readable due to extra logic.

        JonBJ 1 Reply Last reply
        0
        • S Stepan K.

          @JonB exactly - and thank You!
          You didn't find any better solution than I did I can see - a thread, singleshot timer or blocking shot time.sleep() with processEvents() repeated calling.

          Btw, sleep methods of the Qthread class are blocking I have found, so there likely isn't any difference in using these to an ordinary time.sleep().. also precision is rather limited (about 1/100sec? in Windows minimal - likely related to CPU context switchitng timer, whereas qtimer appears to support much higher precision, which is often important, too -- that may rule pausing done with time.sleep and processEvents() out for some situations and make qtimer and individual function calls for each of steps the only viable option).

          Well, maybe there really isn't any nicer way to achieve this type of serial processing in Qt, which is kind of shame - again, this is not a roadblock, just makes the code less readable due to extra logic.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Stepan-K said in Equivalent to asyncio.sleep?:

          You didn't find any better solution than I did I can see - a thread, singleshot timer or blocking shot time.sleep() with processEvents() repeated calling.

          I did, and told you to use a local QEventLoop with exec() instead of processEvents(). Don't know why you ignored that. That is really the canonical way of doing a "sleep/pause" with Qt event loop processing still allowed.

          Btw, sleep methods of the Qthread class are blocking I have found, so there likely isn't any difference in using these to an ordinary time.sleep()..

          I don't understand. You use a "sleep" which blocks the thread --- just what you want between your "command pipeline" being executed in a thread --- but that is fine. The main thread still services the event loop. It's fine to block threads.

          As for time.sleep() precision, if it's not good enough then use a QTimer. That is what I suggested with the QEventLoop::exec() approach.

          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