class SomeThing(object):
def __init__(self, name):
self.name = name
#to parallelizing
os.environ['FOO'] = "http://localhost:1135" + str(self.name) + '/'
def run(self):
...
每次我创建具有不同名称的对象时,我都希望这样做。 (名称是' int'输入),我的对象使用不同的环境变量运行。我试着打电话如下:
some2 = Something(9)
some1 = Something(1)
#job2 = lambda: some2.run()
t2 = threading.Thread(target=some2.run(), name = 'job2')
t2.start()
#job1 = lambda: some1.run()
t1 = threading.Thread(target=some1.run(), name = 'job1')
t1.start()
但两个线程都在使用some2的环境变量(第一个创建的对象的变量)。
答案 0 :(得分:1)
问题解决了,这是代码:
class SomeThing(object): def __init__(self, name): self.name = name def run(self): #to parallelizing os.environ['FOO'] = "http://localhost:1135" + str(self.name) + '/'
和
some2 = Something(9)
some1 = Something(1)
t2 = multiprocessing.Process(target=some2.run())
t1 = multiprocessing.Process(target=some1.run())
t1.start()
t2.start()