简体   繁体   English

如何为python多处理实现非阻塞管理器

[英]How to implement non blocking manager for python multiprocessing

I am trying to set up a manger for multiple python processes.我正在尝试为多个 python 进程设置一个管理器。 My setup is currently a server process, N worker_processes and 1 expensive_item_getter process.我的设置是当前一个server进程,N worker_processes和1个expensive_item_getter过程。 I would like the worker processes to request resources from the manager that may take some time to acquire, without blocking the manager.我希望工作进程从经理那里请求可能需要一些时间才能获取的资源,而不会阻塞经理。 Many of the worker processes will request the same resources, and so these are cached in the manager, but will likely request the same resource before the first request has been handled and the result cached.许多工作进程将请求相同的资源,因此这些资源被缓存在管理器中,但可能会在第一个请求被处理并缓存结果之前请求相同的资源。 I am sure this has been done before and there is a solution but it is currently eluding me.我确信这之前已经完成并且有一个解决方案,但它目前正在逃避我。

  • Ideally with code compatibility for python 3.4+ but not essential.理想情况下与 python 3.4+ 的代码兼容,但不是必需的。

  • Manager should not block on expensive calls but should eventually return the requested resource to the correct process and any other processes that have requested the same resource since the operation started.管理器不应阻塞昂贵的调用,而应最终将请求的资源返回给正确的进程以及自操作开始以来请求相同资源的任何其他进程。

  • There must be a way to do this without manually creating and managing lots of queues and lists with callbacks.必须有一种方法可以做到这一点,而无需手动创建和管理大量带有回调的队列和列表。

  • EDIT I would consider other libraries but would have to be cross platform compatible编辑我会考虑其他库,但必须跨平台兼容

I have tried to outline my intent below.我试图在下面概述我的意图。


import multiprocessing

def worker_process(manager):#runs in own process
    item = manager.get_expensive_item("item_name") # this part can be, and currently is be blocking in this process
    
    
def expensive_item_getter(item_name): # also runs in own process or thread
    #lots of expensive disk io and GPU time
    return item

class MyManager(multiprocessing.managers.BaseManager):
    
    def __init__(self):
        ##register methods
        pass
    
    def get_expensive_item(self, item_name):
        if item_is_cached:
            return cached_item
        else:
            item = expensive_item_getter(item_name)  #this part shouldnt block the server process
            cache_item(item)
            return item

Have you tried ?你有没有尝试过 ? synchronization-between-processes in python python中的进程间同步

I have used a list of Locks我使用了一个锁列表

  if not item_name in self.locks:
    self.locks[item_name ] = Lock()


  with self.locks[item_name]:
    if item_name in cache:
      return cached_item
    else:
      item = expensive_item_getter(item_name)  #this part shouldnt block the server process
      cache_item(item)
      return item

I have never worked with BaseManager so i don't know if the calls them selfs to get_expensive_items are blocking the entire manager.我从未与 BaseManager 合作过,所以我不知道对 get_expensive_items 的调用是否阻塞了整个管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM