Skip to main content

Synchronization primitives specifically designed for use with Asyncio Coroutines, inspired by Rust.

Project description

IronFence Python Library Documentation

Introduction

IronFence is a Python library that provides two types of synchronization primitives: Mutex and RWLock. These objects allow you to protect shared data from concurrent access in an asynchronous environment. By using Mutex and RWLock, you can ensure that your application remains coroutine-safe and avoids race conditions[^1].

[^1]: IronFence doesn't protect against inter-process or intra-thread races. Use additional sync mechanisms like multiprocessing.Lock or threading.Lock for added protection.

Installation

When pip is available, the distribution can be downloaded from PyPI and installed in one step:

pip install ironfence

Mutex

A Mutex (ashort for "mutual exclusion") is a synchronization primitive that allows only one coroutine to access a shared resource at a time. Other coroutines that attempt to acquire the lock will block until the current owner releases it.

import asyncio

import ironfence

shared_state = {}
mu = ironfence.Mutex(shared_state)


async def modify_state():
    print("modifying state..")
    async with mu.lock() as value:
        await asyncio.sleep(1)
        value["key"] = "example"
        print("State modified!")


async def read_state():
    print("reading state..")
    async with mu.lock() as value:
        print("State is:", value)


# run concurrently
asyncio.get_event_loop().run_until_complete(
    asyncio.gather(read_state(), modify_state(), read_state())
)

In this example, we create a Mutex instance named mu that guards a shared dictionary called shared_state. We define two coroutines, modify_state() and read_state(), that need to access shared_state. To ensure exclusive access, we use the lock() method of the Mutex object to acquire the lock before modifying or reading the shared data. The lock() method returns a context manager that automatically releases the lock when exiting the scope.

RWLock

An RWLock (short for "reader-writer lock") is a synchronization primitive that allows multiple reader coroutines to access a shared resource simultaneously, while allowing only one writer coroutine to modify the resource at a time. This makes it more efficient than a Mutex in situations where there are many reader coroutines and only occasional writes.

import asyncio

import ironfence


async def read_state(rw_lock):
    async with rw_lock.read() as value:
        print(value)


async def modify_state(rw_lock):
    async with rw_lock.write() as value:
        value.append("example")


async def main():
    shared_state = []
    rw_lock = ironfence.RWLock(shared_state)
    await asyncio.gather(
        read_state(rw_lock), modify_state(rw_lock), read_state(rw_lock)
    )


asyncio.get_event_loop().run_until_complete(main())

In this example, we create an RWLock instance named rw_lock that guards a shared list called shared_state. We define two coroutines, read_state() and modify_state(), that need to access shared_state. To ensure exclusive access, we use the read() and write() methods of the RWLock object to acquire the appropriate locks. The read() method acquires a read lock, which allows multiple reader coroutines to enter, while the write() method acquires a write lock, which blocks all reader coroutines until the write operation completes.

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

ironfence-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ironfence-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ironfence-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for ironfence-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7b0d7bcd5d9e378bcc63071c354e073a39eb266974563997b049d227d51a4156
MD5 031d22dc4e6ee37a33d96977ac6c3882
BLAKE2b-256 622188f52eb33fa496fa9a3f6fc999ca5d6db680ee5c5636f59e592b575dea74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ironfence-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for ironfence-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddb36406f23118bbca76445bae01e5d34ed6afd60aba9e35551e298134712d53
MD5 934a2685ccb749cce04ab99ecf8b92c3
BLAKE2b-256 75809a9f0a48f58a1dae661bd8e095af5a6eb49a315e01e5e788a885be16b0a0

See more details on using hashes here.

Supported by

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