Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can time.sleep(secs) suspend QNetworkAccessManager does request asynchronously?

QNetworkAccessManager can do requests asynchronously, and time.sleep(secs) can suspend the execution for the given number of seconds. I was confused by the code below. Is t2 here always greater than 10 seconds?

Without using time.sleep(secs) in the code here, the finished slot getWebPageSRC was called in fixed amount of time, roughly about 3 seconds.

I have tested this now a couple of times and found that t2 is always greater than 10 seconds. Can anyone 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)

P.S. since QNAM doing its work asynchronously, I think it works in another thread ,thus has its own event loop , so does time.sleep(secs) suspend all the Qt event loop of all threads or just the event loop of the thread it within ? Does sleeping in main thread suspend all other threads ' event loop ?

like image 412
iMath Avatar asked Dec 21 '14 14:12

iMath


3 Answers

This question seems to be theoretical as it ought to never be a concern in the practice because that smells like fishy design or a quick workaround for a bug.

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

This would not be an issue if you, say, slept in another thread, although even that would be overly strange at first, but here you sleep in the thread (block) where the event is supposed to be handled asynchronously.

It does not make much sense in a Qt application to sleep after all. Qt is primarily meant for async operation, especially the QIODevice interfaces like QtNetwork.

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

time.sleep(10)

Whenever you consider to block to wait for the reply, you can use the sync API, although even that is not completely sync to be fair:

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

I will probably even go further than that: I would probably avoid using Qt in a python application for anything else than strictly the UI. That is, all the rest can be achieved by python means, usually better and easier from a python application. I think you ought to focus on the GUI, but this is somewhat opinion based, for sure. Relevant alternatives would be asyncore, twisted, etc.

like image 190
lpapp Avatar answered Nov 27 '22 16:11

lpapp


Regardless of QNAM doing its work asynchronously, it still operates in main thread. Stopping main thread for 10s blocks QNAM too.

like image 36
Jerry Avatar answered Nov 27 '22 16:11

Jerry


It's true that QNetworkAccessManager can do requests asynchronously but they all get executed in the application main thread. So when you call

time.sleep(10)

The main thread is blocked for 10 seconds and during this blocking nothing else is done. That's because QNetworkAccessManager here is not in an other thread.

like image 31
Nejat Avatar answered Nov 27 '22 18:11

Nejat