Re: Using fcntl.flock with gevent

197 views
Skip to first unread message

vitaly

unread,
Feb 8, 2013, 4:49:49 AM2/8/13
to gev...@googlegroups.com
On Thursday, February 7, 2013 1:17:43 AM UTC-8, Sebastien Estienne wrote:
How to make fcntl.flock to cooperate with gevent?

One way is to leverage a thread-pool in a similar way to how gevent 1.x uses a thread pools for some blocking I/O operations.

Damien Churchill

unread,
Feb 8, 2013, 4:52:25 AM2/8/13
to gev...@googlegroups.com
On 7 February 2013 09:17, Sebastien Estienne
<sebastien...@gmail.com> wrote:
> How to make fcntl.flock to cooperate with gevent?

You could use fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) which
would raise an exception. You could then wrap this in a while loop if
you wanted to replicate the blocking behaviour using something along
the lines of:

while True:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
gevent.sleep(0.1) # arbitrary number

Sebastien Estienne

unread,
Feb 11, 2013, 7:19:15 PM2/11/13
to gev...@googlegroups.com
Thanx Damien, that's the solution that i implemented.

Sebastien Estienne

unread,
Feb 11, 2013, 7:20:03 PM2/11/13
to gev...@googlegroups.com
Hi Vitaly,

Could you elaborate or give an example?

vitaly

unread,
Feb 11, 2013, 7:53:31 PM2/11/13
to gev...@googlegroups.com


On Monday, February 11, 2013 4:20:03 PM UTC-8, Sebastien Estienne wrote:
Hi Vitaly,

Could you elaborate or give an example?

Hi Sebastien, I was thinking of using a gevent-friendly/compatible threadpool just like the os.py module does (https://github.com/SiteSupport/gevent/blob/master/gevent/os.py) - e.g., tp_read, tp_write. I haven't tried this myself, but it seemed like using a gevent-compatible threadpool might solve the blocking issue, and would also avoid the latency that you might get from the other proposed solution that was based on polling.
Reply all
Reply to author
Forward
0 new messages