Python2.3有没有多处理类型的模块?我坚持使用2.3作为我与之接口的程序,并希望能够设置一些多处理,因为我做的任务只使用一个CPU,效率真的很低。
我希望每个线程/进程处理自己的全局变量,并且每个线程/进程不应该与任何其他线程/进程共享任何变量。基本上,我只想有一个需要通过函数运行的文件队列,每次运行都是一个全新的线程。
我试过使用thread.start_new_thread
,但它把我的全局变量弄得一团糟。
我突然想到,我可以从每个新线程执行一个os.popen('python C:\function_dir\function.py vars...')
吗?听起来相当难看,但我不明白为什么它不能工作。在os.popen
“线程”正确结束之前,主程序不会继续运行?
有什么我可能忽略的想法或模块吗?
发布于 2012-08-02 21:50:07
我在任何地方都没有找到,后来我转向了python2.5
发布于 2012-12-10 15:34:22
使用线程。您只需基于线程构建一个类
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()
确保您的所有变量都保持在本地。如果需要访问全局变量,请使用 global 语句:
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
https://stackoverflow.com/questions/6255935
复制相似问题