jplana / python-etcd

A python client for etcd
Other
520 stars 210 forks source link

Lock object seems to not provide locking #258

Open bmbouter opened 6 years ago

bmbouter commented 6 years ago

With aioetcd==0.4.5.0 I can't seem to get working Locks. This is my first version of usage so I'm not sure I can't speak to if this ever worked.

If I have two processes both which try to aquire the same lock with this code:

import asyncio
import os
import time

import aio_etcd as etcd
from aio_etcd.lock import Lock

client = etcd.Client()

async def some_coroutine():
    async with Lock(client, 'mylock') as my_lock:
        print('lock_acquired in pid {pid}'.format(pid=os.getpid()))
        time.sleep(10)
    print('lock_released in pid {pid}'.format(pid=os.getpid()))

loop = asyncio.get_event_loop()
loop.run_until_complete(some_coroutine())

OK then I run the code in parallel using two terminals each running python3 foo.py immediately after each other. If locking is working I expect to see something like this:

PID 1 acquire the lock
# 10 seconds pass
PID 1 release the lock
PID 2 acquire the lock
# 10 seconds pass
PID 2 release the lock

Instead I see something like this:

PID 1 acquire the lock
PID 2 acquire the lock
# 10 seconds pass
PID 1 release the lock
PID 2 release the lock

Does this work for others users? Am I doing this wrong? Any insight into how to fix, I could send a PR with a little guidance.