无法进行多处理以同时运行进程

Bri*_*ley 6 python concurrency multithreading multiprocess

下面的代码似乎并不同时运行,我不确定为什么:

def run_normalizers(config, debug, num_threads, name=None):

    def _run():
        print('Started process for normalizer')
        sqla_engine = init_sqla_from_config(config)
        image_vfs = create_s3vfs_from_config(config, config.AWS_S3_IMAGE_BUCKET)
        storage_vfs = create_s3vfs_from_config(config, config.AWS_S3_STORAGE_BUCKET)

        pp = PipedPiper(config, image_vfs, storage_vfs, debug=debug)

        if name:
            pp.run_pipeline_normalizers(name)
        else:
            pp.run_all_normalizers()
        print('Normalizer process complete')

    threads = []
    for i in range(num_threads):
        threads.append(multiprocessing.Process(target=_run))
    [t.start() for t in threads]
    [t.join() for t in threads]


run_normalizers(...)
Run Code Online (Sandbox Code Playgroud)

config变量只是的以外定义的词典_run()功能.似乎创建了所有进程 - 但它并不比使用单个进程更快地完成.基本上,run_**_normalizers()函数中发生的是从数据库中的队列表(SQLAlchemy)读取,然后发出一些HTTP请求,然后运行规范化器的"管道"来修改数据,然后将其保存回数据库.我来自JVM领域,其中线程"很重"并经常用于并行 - 我有点困惑,因为我认为多进程模块应该绕过Python的GIL的限制.

Bri*_*ley 3

解决了我的多处理问题 - 并且实际上切换了线程。不确定到底是什么解决了它的想法 - 我只是重新架构了所有内容并制作了工作人员和任务以及没有的东西,现在一切都在飞速发展。这是我所做的基础知识:

import abc
from Queue import Empty, Queue
from threading import Thread

class AbstractTask(object):
    """
        The base task
    """
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def run_task(self):
        pass

class TaskRunner(object):

    def __init__(self, queue_size, num_threads=1, stop_on_exception=False):
        super(TaskRunner, self).__init__()
        self.queue              = Queue(queue_size)
        self.execute_tasks      = True
        self.stop_on_exception  = stop_on_exception

        # create a worker
        def _worker():
            while self.execute_tasks:

                # get a task
                task = None
                try:
                    task = self.queue.get(False, 1)
                except Empty:
                    continue

                # execute the task
                failed = True
                try:
                    task.run_task()
                    failed = False
                finally:
                    if failed and self.stop_on_exception:
                        print('Stopping due to exception')
                        self.execute_tasks = False
                    self.queue.task_done()

        # start threads
        for i in range(0, int(num_threads)):
            t = Thread(target=_worker)
            t.daemon = True
            t.start()


    def add_task(self, task, block=True, timeout=None):
        """
            Adds a task
        """
        if not self.execute_tasks:
            raise Exception('TaskRunner is not accepting tasks')
        self.queue.put(task, block, timeout)


    def wait_for_tasks(self):
        """
            Waits for tasks to complete
        """
        if not self.execute_tasks:
            raise Exception('TaskRunner is not accepting tasks')
        self.queue.join()
Run Code Online (Sandbox Code Playgroud)

我所做的就是创建一个 TaskRunner 并向其中添加任务(数千个),然后调用 wait_for_tasks()。所以,显然在我所做的重新架构中,我“修复”了我遇到的其他一些问题。虽然很奇怪。