2

Python 2.3是否有任何多处理类型模块?我使用2.3接口的程序卡住了,并希望能够设置一些多处理,因为我的任务只使用一个CPU,而且效率很低。Python 2.3多重处理

我想每个线程/进程处理自己的全局变量,每个线程/进程不应该与任何其他线程/进程共享任何变量。基本上我只想有一个需要通过函数运行的文件队列,每次运行都将是一个全新的线程。

我试过使用thread.start_new_thread,但它变成了一个混乱的全局变量。

我刚想到一个想法,我可以从每个新线程执行os.popen('python C:\function_dir\function.py vars...')吗?听起来很丑,但我不明白为什么它不起作用。主程序将不会继续,直到os.popen“线程”完成正确?

我可能忽略的任何想法或模块?我没有发现任何地方

回答

0

没有,因为我已经转移到Python 2.5的

0

使用线程。您只需根据主题建立一个类:

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() 

请务必让所有的局部变量。如果您需要访问全局变量使用全球声明:

counter = 0 

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

为了锁定等必须在Python文档看看还有:http://docs.python.org/release/2.3.5/lib/module-threading.html