Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spawning a thread in python

Tags:

I have a series of 'tasks' that I would like to run in separate threads. The tasks are to be performed by separate modules. Each containing the business logic for processing their tasks.

Given a tuple of tasks, I would like to be able to spawn a new thread for each module as follows.

from foobar import alice, bob charles data = getWorkData() # these are enums (which I just found Python doesn't support natively) :( tasks = (alice, bob, charles)  for task in tasks   # Ok, just found out Python doesn't have a switch - @#$%!   # yet another thing I'll need help with then ...   switch     case alice:       #spawn thread here - how ?       alice.spawnWorker(data) 

No prizes for guessing I am still thinking in C++. How can I write this in a Pythonic way using Pythonic 'enums' and 'switch'es, and be able to run a module in a new thread.

Obviously, the modules will all have a class that is derived from a ABC (abstract base class) called Plugin. The spawnWorker() method will be declared on the Plugin interface and defined in the classes implemented in the various modules.

Maybe, there is a better (i.e. Pythonic) way of doing all this?. I'd be interested in knowing

[Edit]

I've just been reading a bot more and it seems Python does not implement threading in the true sense (at least, not in the sense that a C++ programmer would think). In any case thats not a show stopper for me. Each of the tasks are fairly time consuming, and I dont want to hold up starting one task until another has completed, thats why I am using threading. Time slicing does not bother me much - so long as they are all started pretty much at the same time (or shortly after each other) Python can then timeslice between the treads as much as it wants - its fine by me.

I have seen an answer to a similar question here on SO.

A user provides a simple class for threading as follows:

import threading class Foo (threading.Thread):     def __init__(self,x):         self.__x = x         threading.Thread.__init__(self)     def run (self):           print str(self.__x)  for x in xrange(20):     Foo(x).start() 

I am thinking of using this for my ABC Plugin. My question then is where do I put the code where the actual task gets done (i.e. the business logic). I assume this goes in the run() method of the Foo class (obvious question I know, but I dont want to make any assumptions).

Is my thinking on the right track or flawed (if flawed - what have I missed?)

like image 227
morpheous Avatar asked May 21 '10 13:05

morpheous


People also ask

How do you spawn a thread in Python?

Creating Thread Using Threading ModuleDefine a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.

What is spawning a thread?

Spawning a new physical thread will mean a transition into Kernel mode and setting up a bunch of mernel structures, so the overhead will be higher.

Can threads spawn threads Python?

Yes, spawning threads from within another thread is OK. Just remember that there exists a main Python thread, that governs the others.


1 Answers

Instead of switch-case, why not use a proper polymorphism? For example, here what you can do with duck typing in Python:

In, say, alice.py:

def do_stuff(data):     print 'alice does stuff with %s' % data 

In, say, bob.py:

def do_stuff(data):     print 'bob does stuff with %s' % data 

Then in your client code, say, main.py:

import threading import alice, bob  def get_work_data():     return 'data'  def main():     tasks = [alice.do_stuff, bob.do_stuff]     data = get_work_data()     for task in tasks:         t = threading.Thread(target=task, args=(data,))         t.start() 

Let me know if I need to clarify.

like image 72
Santa Avatar answered Nov 07 '22 04:11

Santa