Converting existing module/objects to threads
John Henry
john106henry at hotmail.com
Thu Oct 19 00:25:00 EDT 2006
Making your code run in thread mode isn't the hard part. Just add
this:
import threading
class subcontrollerThread(threading.Thread, subcontroller):
def __init__(self,id,configurationFile):
threading.Thread.__init__(self)
subcontroller.__init__(self,id,configurationFile)
def run(self):
self.process()
threads=[]
# Say we have 5 of the subprocesses
for iThread in range(5):
th=subcontrollerThread(iThread,configurationFile)
threads.append(th)
th.start()
...main thread do whatever...
However, you have to make sure the code inside subcontroller is thread
safe. That's a topic in itself.
jdlists at gmail.com wrote:
> I have inheirted some existing code, that i will explain in a moment,
> have needed to extend and ultimately should be able to run in threads.
> I've done a bunch of work with python but very little with threads and
> am looking for some pointers on how to implement, and if the lower
> level modules/objects need to be rewritten to use threading.local for
> all local variables.
>
> I have a module that communicates with a hardware device, which reads
> data off of sensors, that can only talk with one controller at a time.
> The controller (my module) needs to (in its simplest form) init,
> configure the device, request data, and write out xml, sleep, repeat.
>
> The new request is that the device needs to be queried until a
> condition is true, and then start requesting data. So an instance of a
> controller needs to be deadicated to a hardware device forever, or
> until the program ends....which ever comes first.
>
> This currently works in a non-threaded version, but only for one device
> at a time, there is a need to create a single windows(yeach) service
> that talks to many of these devices at once. I don't need worker
> threads that handle seperate portions of the entire job, i need a
> single application to spawn multiple processes to run through the
> entire communication from configure to report, sleep until the next
> interval time and run again. The communication could last from 1
> minute to 10 minutes before it ends.
>
>
> Here is the code layout in pseudocode.
>
> module.Object - controller.Main - handles all socket communications
>
> class subcontroller(controller.Main):
> def __init__(self,id,configurationFile):
> controller.Main.__init__(self)
> // instantiate variables and local objects that handle
> configuration, logic and data output
>
> def configure(self,configurationFile):
> //read configurationFile and configure device
>
> def process(self):
> while 1:
> //based on configuration file, query the device until condition
> is true and then write xml, sleep until time to repeat and run again.
>
> within controller there are 5 objects and subcontroller is a sinlge
> object that loads other objects from the inherited controller.System
>
> I'm trying to figure out how difficult it is going to be to convert
> this to a threaded application. The original controller.Main is built
> to talk to devices in series, never in parallel. so no objects are
> considered to be thread safe, but no instance of any of the objects
> should need to share resources with any other instance of teh same
> object. they would all have unique configuration files and talk to
> devices on unique ip/ports.
>
> on a unix system, forking,while potentially not optimal, would be a
> fine solution, unfortunantely this needs to run on windows.
>
> I know i have left out many details, but hopefully this is enough to at
> least enable some kind soles to lend an opinnion or two.
>
> many thanks
> jd
More information about the Python-list
mailing list