Python multiprocessing Manager로 custom class 공유하기
BaseManager에 커스텀 클래스를 register하면 된다. 나머지는 그냥 원래 Manager 쓰는 거랑 똑같이 쓰면 된다.
import asyncio
import aiohttp
from multiprocessing.managers import BaseManager
class Session:
def __init__(self, beta_engine=False):
process = self._init_session()
asyncio.run(process)
async def _init_session(self):
self.session = aiohttp.ClientSession()
class MyManager(BaseManager):
pass
MyManager.register('Session', Session)
with MyManager() as manager:
session = manager.Session()
import multiprocessing
from multiprocessing.managers import BaseManager, NamespaceProxy
class Counter(object):
def __init__(self):
self.value = 0
def update(self, value):
self.value += value
def update(counter_proxy, thread_id):
counter_proxy.update(1)
class CounterManager(BaseManager):
pass
class CounterProxy(NamespaceProxy):
_exposed_ = ('__getattribute__', '__setattr__', '__delattr__', 'update')
def update(self, value):
callmethod = object.__getattribute__(self, '_callmethod')
return callmethod(self.update.__name__, (value,))
CounterManager.register('Counter', Counter, CounterProxy)
def main():
manager = CounterManager()
manager.start()
counter = manager.Counter()
pool = multiprocessing.Pool(multiprocessing.cpu_count())
for i in range(10):
pool.apply(func = update, args = (counter, i))
pool.close()
pool.join()
print 'Should be 10 but is %s.' % counter.value
if __name__ == '__main__':
main()
출처는 stackoverflow