Skip to main content

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.

Source Distribution

prwlock-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distributions

prwlock-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

prwlock-0.1.0-py2-none-any.whl (7.2 kB view details)

Uploaded Python 2

File details

Details for the file prwlock-0.1.0.tar.gz.

File metadata

  • Download URL: prwlock-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for prwlock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 332c1481ad1b4035a4679c6e998e91c5f5db7864bc297575cc3a02f086ce342b
MD5 544af66874185f4e4809378e9fe6b195
BLAKE2b-256 e65c80ce26e81eadaf7a66adc636d46bd01cfa65f7969967d88eb40efaa09372

See more details on using hashes here.

File details

Details for the file prwlock-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prwlock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 192130cc66674d9a08a18ecf9ea6de9748ca707b876d130c60d89b6b7fb4e86c
MD5 ab5c8fd78e1442148a8acde4f2676a76
BLAKE2b-256 dd6ced53897db1d196b5189837ad9a34549ba934b3b5f79f0339e1a70683bdba

See more details on using hashes here.

File details

Details for the file prwlock-0.1.0-py2-none-any.whl.

File metadata

File hashes

Hashes for prwlock-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 c745be90fb73ba3b80209f33b587759a19bdd1bef1915db5c52bef8beb30a0b9
MD5 1b26d6fc1beb457aa21a9ef42c4ce683
BLAKE2b-256 27086a5d845e31563b4f3ae4986a71b3f2633c670e6d3d84533388c7fdacfa35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page