python- how to get the output of the function used in Timer

2024/11/13 6:39:34

I want to run a function for 10s then do other stuff. This is my code using Timer

from threading import Timer
import timedef timeout():b='true'return ba='false'    
t = Timer(10,timeout)
t.start()while(a=='false'):print '1'time.sleep(1)
print '2'

I know that using Timer I can print something at the end of the timer (print b instead of return b return true after 10s). What I want to know is : can I get the value returned by timeout() inside "a" to perform my while loop correctly?

Or is there another way to do it with another function ?

Answer

The return value of the function is just dropped by Timer, as we can see in the source. A way to go around this, is to pass a mutable argument and mutate it inside the function:

def work(container):container[0] = Truea = [False]
t = Timer(10, work, args=(a,))
t.start()while not a[0]:print "Waiting, a[0]={0}...".format(a[0])time.sleep(1)print "Done, result: {0}.".format(a[0])

Or, use global, but that's not the way a gentleman goes.

https://en.xdnf.cn/q/72215.html

Related Q&A

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…

Pull Tag Value using BeautifulSoup

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:<span title="Funstuff&qu…

What is the practical difference between xml, json, rss and atom when interfacing with Twitter?

Im new to web services and as an introduction Im playing around with the Twitter API using the Twisted framework in python. Ive read up on the different formats they offer, but its still not clear to m…

how to grab from JSON in selenium python

My page returns JSON http response which contains id: 14Is there a way in selenium python to grab this? I searched the web and could not find any solutions. Now I am wondering maybe its just not poss…

Numpy: Array of `arange`s

Is there a way to take...>>> x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1)); ncols = 5...and turn it into...array([[ 0, 1, 2, 3, 4],[ 8, 9, 10, 11, 12],[10, 11, 12, 13, 14],[15, 16, 17…

Understanding model.summary Keras

Im trying to understand model.summary() in Keras. I have the following Convolutional Neural Network. The values of the first Convolution are: conv2d_4 (Conv2D) (None, 148, 148, 16) 448 …

Determine adjacent regions in numpy array

I am looking for the following. I have a numpy array which is labeled as regions. The numpy array represents a segmented image. A region is a number of adjacent cells with the same value. Each region h…

Python: win32gui.SetForegroundWindow

I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I can…

Building PyCrypto with fastmath (gmp or mpir) via pip on Windows

I installed PyCrypto on Windows via pip but i was not able to build Crypto.PublicKey._fastmath because GMP was not found.I know there is a binary version on voidspace but i would like to build the late…