使用对象进行多处理?

时间:2017-02-14 13:59:30

标签: python python-multiprocessing

给出以下代码:

if __name__ == '__main__':
    print "name = main"
    multiprocessing.Process(target=r.fetchFiles, args=(r.results.pop(),))

class R:
    # ...
    def fetchFiles(self, blabla):
        with open('/tmp/doyou.txt', 'w+') as f:
            f.write('do you do something?')

rR的一个实例。测试文件没有写入。为什么呢?

1 个答案:

答案 0 :(得分:1)

流程必须为started

process = multiprocessing.Process(target=r.fetchFiles, args=(r.results.pop(),))
process.start()
process.join()

一个简单的工作示例:

from multiprocessing import Process

class R:
    def print_value(self, value):
        print(value)

if __name__ == '__main__':
    process = Process(target=R().print_value, args=('a',))
    process.start()
    process.join()