Can time.sleep (secs) pause QNetworkAccessManager requests asynchronously?

QNetworkAccessManager can execute requests asynchronously, and time.sleep(secs) can pause execution for a given number of seconds. I was confused by the code below. t2 here is always more than 10 seconds?

Without using time.sleep(secs) in this code, the final getWebPageSRC slot getWebPageSRC called at a fixed time, about 3 seconds.

I tested this now a couple of times and found that t2 is always more than 10 seconds. Can someone explain why?

 de myMethod(self): ... reply.finished.connect(self.getWebPageSRC) self.t=time.clock() time.sleep(10) def getWebPageSRC(self): t2=time.clock()-self.t print(t2) 

PS since QNAM does its work asynchronously, I think it runs on a different thread, so it has its own event loop, so that time.sleep (secs) pauses the entire Qt event loop for all threads, or only its thread's event loop inside? Does hibernation in the main thread suspend the event loop of other threads?

+5
source share
3 answers

This question seems theoretical, since it should never be a problem in practice, because it smells like a fishy design or a quick workaround for a mistake.

Having said that, the reason is relatively simple: when you start to sleep, the Qt event loop cannot do its job, so your slot cannot be processed from the event queue by the event loop before you wake up from your blocking sleep.

This will not be a problem if you, say, were sleeping in another thread, although even that would be too strange at first, but here you are sleeping in a thread (block) where the event should be processed asynchronously.

There is not much point in sleeping in a Qt application. Qt is primarily intended for asynchronous operation, especially QIODevice interfaces such as QtNetwork.

When using Qt, forget about the existence of this statement :

 time.sleep(10) 

Whenever you plan to block waiting for a response, you can use the synchronization API, although even this is not fully synchronized to be fair:

 # 10000 msecs = 10 secs myNetworkReply.waitForBytesWritten(10000) 

I will probably even go further: I would probably not use Qt in a python application for anything other than a strictly user interface. That is, everything else can be achieved using python tools, usually better and easier with a python application. I think you should focus on the graphical interface, but this is certainly an opinion based opinion. Appropriate alternatives will be asynchronous, twisted, etc.

+4
source

Regardless of how QNAM does its work asynchronously, it still works in the main thread. Main thread stop for 10 second QNAM blocks too.

+3
source

It is true that QNetworkAccessManager can execute requests asynchronously, but all of them are executed in the main thread of the application. Therefore when you call

 time.sleep(10) 

The main thread is blocked for 10 seconds, and nothing is done during this lock. This is because QNetworkAccessManager is not in another thread here.

+2
source

Source: https://habr.com/ru/post/1209657/


All Articles