开发者

Python 2.3 multiprocessing

开发者 https://www.devze.com 2023-03-11 00:42 出处:网络
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 CP

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 a开发者_运维知识库nd 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消