python - multiprocessing.dummy池功能,以提高查询执行时间

时间:2017-04-21 12:12:35

标签: python multiprocessing

我有一个oracle查询,它返回了大约300万行,看起来像这样:

select key from table;---just one column

我试图使用python中的multithreading.dummy模块中的池对象并行执行此操作

import threading
local = threading.local()

def execute_query:
    if not hasattr(local, 'db'):
         local.db = params.get('conn')
         cursor = local.db.cursor()
         cursor.execute(params.get('sql'))
         return cursor.fetchall()

并且,为了得到上面提到的并行执行的oracle查询,我正在做:

def __call__(self):
    pool = Pool(10)
    data = pool.apply(execute_query, [{ 'conn' :   self._connection, 'sql': self._sql }])

    pool.close()
    pool.join()

我知道apply method的{​​{1}}只在一个工作线程中执行传递的函数。

有什么方法可以在多个线程中执行此pool object以提高性能?

1 个答案:

答案 0 :(得分:0)

  

问题:我试图并行执行此操作。

apply(func[, args[, kwds]])

Call func with arguments args and keyword arguments kwds.
It blocks until the result is ready.
Given this blocks, apply_async() is better suited for performing work in parallel.
Additionally, func is only executed in one of the workers of the pool.
相关问题