Skip to main content

Parallel Iteration with File-Based Coordination

Project description

codecov

Laufband: Parallel Iteration with File-Based Coordination

Laufband enables parallel iteration over a dataset from multiple processes, utilizing file-based locking and communication to ensure each item is processed exactly once.

Installation

Install Laufband using pip:

pip install laufband

Usage

Using Laufband is similar to the familiar tqdm progress bar for sequential iteration.

from laufband import laufband

data = list(range(100))
for item in laufband(data):
    # Process each item in the dataset
    pass

The true power of Laufband emerges when you run your script in parallel. Multiple processes will coordinate using file-based locking to ensure that each item in the dataset is processed by only one process.

Here's a typical example demonstrating parallel processing with Laufband and file-based locking for shared resource access:

import json
import time
from pathlib import Path
from flufl.lock import Lock
from laufband import laufband

output_file = Path("data.json")
output_file.write_text(json.dumps({"processed_data": []}))
data = list(range(100))
lock = Lock("laufband.lock")

for item in laufband(data, lock=lock, desc="using Laufband"):
    # Simulate some computationally intensive task
    time.sleep(0.1)
    with lock:
        # Access and modify a shared resource (e.g., a file) safely using the lock
        file_content = json.loads(output_file.read_text())
        file_content["processed_data"].append(item)
        output_file.write_text(json.dumps(file_content))

To execute this script (main.py) in parallel, you can use a command like the following in your terminal (this example launches 10 background processes):

for i in {1..10} ; do python main.py & done

[!IMPORTANT] The different processes may finish at different times. Therefore, the order of items in file_content is not guaranteed. If the order is important, you will need to implement sorting logic afterwards.

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

laufband-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

laufband-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: laufband-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.17

File hashes

Hashes for laufband-0.1.0.tar.gz
Algorithm Hash digest
SHA256 18f7a30dbbb7b5ab8a13e8479b8dc96fb9db4256debbbeac741bf56e802b6e27
MD5 09f6a937a93ee1d86ca0765b0e3f9a39
BLAKE2b-256 cc58b71f04b34989aef3d697993dba0fcb3ac141e827ff0ec34b3a49f6dacb94

See more details on using hashes here.

File details

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

File metadata

  • Download URL: laufband-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.17

File hashes

Hashes for laufband-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 831eacd688a2deb192787139c2a0158d8ac8b2bfa2c418c30ffc61eca98dcf64
MD5 875ee8ff39ba12c965e6b19d03cafa54
BLAKE2b-256 ec1844d0b32e818599ac9b2681f8462c97bf9d9671faee4c2319d11b8f6020a7

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