I know, not really the place to post this, but you've been quite helpful before, so.... sorry.
I want to do a task in python in a way that does not block the program. For example, downloading files from the internet.
Of course I could do a separate program and call a subprocess, or do somethink like "os.popen('wget .... &')". But I want to do this inside the main program, in pure python.
Which is the best way to do this? Can you point me to some reading?
But beware, multithreaded programming can get complicated. So if you just need to get something done, subprocess and wget might not be such a bad idea after all...
I generally recommend simplifying thread/lock management.
If you are doing a simple shell script, use the Queue class to retrieve results from the threads.
If you are using gobject or QtCore you deal with callbacks a lot. The problem with that is it spreads the logic of your code out. I've put together some constructs that simplify pushing things off into threads and then getting the results back. If so I can give you some pointers on them.
Sorry, I should have been more clear. I don't want anything too complicated. I just want my app not to get blocked while it downloads some specific files from the internet, since those files are not used by the program itself.
Sorry, I should have been more clear. I don't want anything too complicated. I just want my app not to get blocked while it downloads some specific files from the internet, since those files are not used by the program itself.
Will take a look on threads.
Thanks!
L.
You say app, which implies GUI (rather than script). This adds complication because you have to notify the UI thread that data is available. I have code to simplify this for both Qt and GTK. Interested?
You say app, which implies GUI (rather than script). This adds complication because you have to notify the UI thread that data is available. I have code to simplify this for both Qt and GTK. Interested?
Yes, my app has a gtk GUI. I only want a dialog to open when the download is finished, but that the download does not block the program.
If you have a code for something like this, yes, I'm interested.
I know, not really the place to post this, but you've been quite helpful before, so.... sorry.
I want to do a task in python in a way that does not block the program. For example, downloading files from the internet.
It depends on your program:
If you have a cpu-intensive program (let's say a password cracker - that runs all the time) that needs to communicate with the network you may need separate processes (but for the cpu-intensive part).
For most other cases you do this using select/poll or an API that is based on that (like asyncore or twisted).
On the other hand, if you're writing a GUI program you'll have to look at the toolkit's libraries for something that properly interacts with its main loop. Qt for example has a networking module that will fit your needs.
"login" creates a AsyncLinearExecution which runs "_login" in the foreground thread but everything at a "yield" in a background thread. Looking at the "_login" function this takes what would be two or three callbacks with different kinds of state and simplifies it to a single function.
EDIT: And since I wrote that code, I've generalized it for both Qt and GTK. Makes callback code a lot simpler. Each piece of code after a "yield" is runs as a callback and you would have had to find a way to pass all that state to that callback.
Probably I didn't understand this, but isn't this something to call an external program? Kind of subprocess.Popen ? I want to run a method of the program itself async.