可调用的多处理脚本

时间:2016-10-24 14:49:31

标签: python multiprocessing python-multiprocessing

我会以this question的答案作为我的起点,因为我觉得这是一个非常简单的例子,但我正在寻找这个问题的一般答案。 我曾经以这种方式在python中进行多处理,其中实际工作所需的一切都封装在if __name__ ...语句中,如下所示:

from multiprocessing import Pool as ThreadPool
import requests


API_URL = 'http://example.com/api'

def foo(x):
  params={'x': x}
  r = requests.get(API_URL, params=params)
  return r.json()

if __name__ == '__main__':
  pool = ThreadPool(4) # Hint...
  num_iter = [1,2,3,4,5]
  out = pool.map(foo, num_iter)
  print(out)

现在,我希望能够创建一个函数,以便能够从另一个python脚本启动多处理部分。但是只要我用一个函数定义替换if语句(或封装它)并尝试运行该函数,我就得到AttributeError: 'module' object has no attribute 'foo'

from multiprocessing import Pool as ThreadPool
import requests


API_URL = 'http://example.com/api'

def foo(x):
  params={'x': x}
  r = requests.get(API_URL, params=params)
  return r.json()

def main():
  pool = ThreadPool(4) # Hint...
  num_iter = [1,2,3,4,5]
  out = pool.map(foo, num_iter)
  print(out)

所以我的问题是,如何以这种方式正确封装多处理,可以方便地从另一个模块,脚本等调用它。?

1 个答案:

答案 0 :(得分:1)

您必须以某种方式使用if __name__=='__main__':

这是mp_test.py

import multiprocessing
import os

def worker():
    """worker function"""
    print 'Worker'
    return

def main(module_name):
    jobs = []
    print "The multiprocessing function in %s is called from %s" %(os.path.basename(__file__),module_name)
    for i in range(5):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

这是main.py

from imsho import *
import os

if __name__ =='__main__':
    main(os.path.basename(__file__))

结果:

The multiprocessing function in mp_test.pyc is called from main.py
Worker
Worker
Worker
Worker
Worker