🚀 Go-like concurrency in Python with channels and true parallelism.
Project description
pygoroutine 🚀
Go-like Concurrency in Python.
pygoroutine brings the simplicity and power of Go's concurrency model—goroutines and channels—to Python. It provides a dead-simple API to make concurrent programming feel effortless and intuitive, whether you're dealing with I/O-bound or CPU-bound tasks.
Key Features
- Dead-Simple Concurrency: Fire-and-forget tasks with a single
go()call. - Go-style Channels: Elegant communication using
ch << valueto send andfor item in ch:to receive. - True Parallelism: Bypass the GIL for CPU-bound tasks with
process=True. - Unified API: Handles
asyncand regular functions automatically. - Robust Lifecycle Management: An optional
GoroutineManagerprovides fine-grained control for libraries and complex applications.
Installation
pip install pygoroutine
Quick Start: The Go-like Way
This example demonstrates the core features: starting a concurrent task with go() and communicating with it over a channel.
import time
from pygoroutine import go, nc
def producer(ch):
"""A producer "goroutine" that sends numbers over a channel."""
print("Producer starting...")
for i in range(5):
message = f"Message #{i+1}"
print(f"-> Sending: '{message}'")
ch << message # Send a value into the channel
time.sleep(0.5)
ch.close()
print("Producer finished.")
def main():
ch = nc()
go(producer, ch)
# The main thread becomes the consumer.
print("Consumer waiting for messages...")
for received_message in ch:
print(f"<- Received: '{received_message}'")
print("Consumer finished. All tasks complete.")
if __name__ == "__main__":
main()
Core Concepts
1. The go() Function
The go() function is the heart of the library. It runs any function or coroutine concurrently without blocking and returns a Future object.
from pygoroutine import go
import time
def my_sync_task(name):
time.sleep(1)
return f"Sync task '{name}' finished."
future = go(my_sync_task, "A")
print("Main thread is not blocked.")
# You can optionally wait for the result
result = future.result()
print(result)
2. Channels for Communication
Channels provide a safe and elegant way for your concurrent tasks to communicate.
- Send:
channel << value - Receive (Loop):
for item in channel: - Receive (Single):
item = channel.get() - Close:
channel.close()
3. True Parallelism for CPU-Bound Tasks
Bypass Python's GIL by running CPU-bound tasks in a separate process with the process=True flag.
from pygoroutine import go
def sum_squares(n):
return sum(i * i for i in range(n))
# This runs in another process, utilizing another CPU core.
future = go(sum_squares, 10_000_000, process=True)
result = future.result()
print(f"Result from process: {result}")
Advanced Usage: The GoroutineManager
For libraries or applications needing explicit setup and teardown, use the GoroutineManager. It provides a context manager for clean, predictable lifecycle management.
from pygoroutine import GoroutineManager
import time
def worker(ch):
time.sleep(0.1)
ch << "done"
with GoroutineManager() as app:
ch = app.nc()
app.go(worker, ch)
result = ch.get()
print(f"Received '{result}' from worker.")
print("Manager has been shut down.")
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pygoroutine-0.1.6.tar.gz.
File metadata
- Download URL: pygoroutine-0.1.6.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e123bc8a1d77c53b060ea5090ddd5fcc039bc2cbe72d12db46b2e5c0a44a8db
|
|
| MD5 |
e03204793a61a4b4a2d6b13dfffe45e1
|
|
| BLAKE2b-256 |
dd7cbd8a9a6c4fbca879e3ebd6f4b39495ad1e085846e581dd3413d79330c62d
|
Provenance
The following attestation bundles were made for pygoroutine-0.1.6.tar.gz:
Publisher:
publish.yml on antonvice/pygoroutine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pygoroutine-0.1.6.tar.gz -
Subject digest:
0e123bc8a1d77c53b060ea5090ddd5fcc039bc2cbe72d12db46b2e5c0a44a8db - Sigstore transparency entry: 454434103
- Sigstore integration time:
-
Permalink:
antonvice/pygoroutine@6d99648d628f9ecdbbf11a6a37e32afe4bd3d0d6 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/antonvice
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6d99648d628f9ecdbbf11a6a37e32afe4bd3d0d6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pygoroutine-0.1.6-py3-none-any.whl.
File metadata
- Download URL: pygoroutine-0.1.6-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbbcdb6c158321bcc13225432a332326cda4d953fee1e0d551a2bb80b84f01a3
|
|
| MD5 |
14050fa0c66223872cfb941967787d72
|
|
| BLAKE2b-256 |
1fa1ec0d078b188e745f7505235fc1bd3b780c8f55df92f9a031396d4f782b23
|
Provenance
The following attestation bundles were made for pygoroutine-0.1.6-py3-none-any.whl:
Publisher:
publish.yml on antonvice/pygoroutine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pygoroutine-0.1.6-py3-none-any.whl -
Subject digest:
bbbcdb6c158321bcc13225432a332326cda4d953fee1e0d551a2bb80b84f01a3 - Sigstore transparency entry: 454434127
- Sigstore integration time:
-
Permalink:
antonvice/pygoroutine@6d99648d628f9ecdbbf11a6a37e32afe4bd3d0d6 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/antonvice
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6d99648d628f9ecdbbf11a6a37e32afe4bd3d0d6 -
Trigger Event:
push
-
Statement type: