2

Is there any multiprocessing type module for Python 2.3? I am stuck using 2.3 for the programs I interface with and would like to be able to setup some multiprocessing as the tasks I do only use one CPU and are really inefficient.

I would like each thread/process to handle its own global variables and each thread/process should not share any variables with any other thread/process. Basically I would just like to have a queue of files that need be run through a function and each run would be an entirely new thread.

I have tried using thread.start_new_thread, but it just turned into a mess with my global variables.

A thought just occurred to me, can I do a os.popen('python C:\function_dir\function.py vars...') from each new thread? Sounds rather ugly, but I don't see why it wouldn't work. The master program wouldn't continue until the os.popen "thread" finishes correct?

Any thoughts or modules I may be overlooking?

agf
  • 171,228
  • 44
  • 289
  • 238
Das.Rot
  • 638
  • 4
  • 11
  • 25

2 Answers2

0

None that I ever found anywhere, I have since moved on to python 2.5

Das.Rot
  • 638
  • 4
  • 11
  • 25
0

Use threading. You simply need to build a class based on Thread:

import threading

class myThread(threading.Thread):
    #
    # Constructor.
    #
    def __init__(self, ...):
        #
        # Call threading constructor.
        #
        threading.Thread.__init__(self)
        #
        # Your constructor code.
        #
        ...
    #
    # The code executed when starting the thread.
    #
    def run(self):
        ...
#
# Create an instance and start the thread.
#
myThread(...).start()

Make sure to keep all your variables local. If you need to access global variables use the global statement:

counter = 0

class myThread(threading.Thread):
    ...
    def run(self):
        global counter
        ...
        counter = 17
...

For locking, etc. have a look at the Python documentation as well: http://docs.python.org/release/2.3.5/lib/module-threading.html

André Caron
  • 44,541
  • 12
  • 67
  • 125
Konrad Holl
  • 616
  • 5
  • 12