Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Native process-shared rwlock support for Python

Project description

https://travis-ci.org/trovao/prwlock.svg https://coveralls.io/repos/trovao/prwlock/badge.svg?branch=master&service=github

A reader-writer lock for Python that can (must, actually) be used for locking across multiple Python processes.

The rationale and initial implementation of the project can be found in the accompanying blog post.

Usage

There is no need for initialization. Therefore, a code block such as the one below is enough to get an RWLock instance.

from prwlock.prwlock import RWLock

rwlock = RWLock()

The RWLock itself is pickleable and, therefore, can be passed around to child processes, such as in the code block below.

from __future__ import print_function

import os
import time

from multiprocessing import Pool
from prwlock.prwlock import RWLock
def f(rwlock):
    for i in range(2):
        print(os.getpid(), 'Acquiring read lock')
        rwlock.acquire_read()
        print(os.getpid(), 'Sleeping for a while')
        time.sleep(1)
        print(os.getpid(), 'Releasing lock')
        rwlock.release()
        time.sleep(.1)

r = RWLock()
children = 20
pool = Pool(processes=children)
for child in range(children):
    pool.apply_async(f, [r])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for prwlock, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size prwlock-0.1.0-py2-none-any.whl (7.2 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size prwlock-0.1.0-py3-none-any.whl (7.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size prwlock-0.1.0.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page