kazoo
kazoo copied to clipboard
kazoo client hangs with multiprocess
kazoo client hangs when I use it within multiprocess.
I'm thinking it's most likely because it's not safe for a child and parent process to share kazoo client.
If that's true, what is the safe way for share a kazoo client within the same process, but still prevent hanging when using multiprocess?
Snippet to Reproduce the Problem
The following code is fine when using single process
client = None
def get_shared_kazoo_client():
""" returns shared kazoo client"""
from kazoo.client import KazooClient
global client
if client is None:
client = KazooClient(hosts=KAZOOCLIENT)
client.start()
return client
def semaphore_function():
client = get_shared_kazoo_client()
throttler = client.Semaphore(max_leases=4, path='/test/path')
with throttler:
logging.info("inside")
if __name__ == '__main__':
semaphore_function()
semaphore_function()
This is fine no matter how many times I call semaphore_function()
.
However, the following code would hang.
if __name__ == '__main__':
semaphore_function()
p = mp.Process(target=semaphore_function)
p.start()
p.join()
The child process starts, but is unable to ever past with throttler
.
I believe this post is observing similar behavior. https://github.com/python-zk/kazoo/issues/519
I would like to have a global shared kazoo client to prevent unnecessarily constructing new kazoo clients over and over again. But, it fails the moment I start multiprocessing.
Any comments would be much appreciated.
Specifications
- Kazoo version: 2.6.1
- Python version: 3.6
- OS: centOS 7
any progress?