创建具有不同环境变量的对象

时间:2018-01-27 12:34:45

标签: python multithreading parallel-processing environment-variables

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的环境变量(第一个创建的对象的变量)。

1 个答案:

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