Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the simplest way to implement fire-and-forget parallel threads in Python?

I have a program with a tkinter GUI (Python 2.7) from which the user can launch some more or less calculation-intense tasks. Most of those result either in a file written to disk or an interactive pyplot window showing results, but none of them feed back into the main task.

I've never done multithreading before and am trying to decide which library to go for. subprocess seems to be for system calls (which this is not), multiprocessing seems to be concerned with parallel execution of larger tasks (with pools and queues and such), and threading ... I've looked through the official documentation but am somewhat unclear how I would use this.

The ideal solution for me would be something that would "simply" phrase the function call which triggers calculating and plotting some data in a way that it will be executed independent of the main program so the user can keep doing their thing without waiting for the function to finish (which usually takes a few seconds up to a minute) -- the plot would just eventually pop up.

Update Turns out the task I wanted to launch in parallel contains bound methods, thus is not picklable and can't be used in parallel. I need to deal with some other business before I'll have time to figure out how to change that -- I'll get back to this, though!

like image 294
Zak Avatar asked Dec 14 '25 18:12

Zak


1 Answers

The multiprocessing library is probably your best bet, just create a process and start it. From the manual: https://docs.python.org/2/library/multiprocessing.html

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()

On exit you probably do want to wait for the results, so add the join() method:

p.join()

Or if you simply want to wait for a short while:

p.join(timeout=1)
like image 109
Wolph Avatar answered Dec 16 '25 12:12

Wolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!