Skip to main content

Identical to unsync, but supports cpu-bound continuation functions

Project description

dunsync

Identical to unsync, except that continuation callbacks receives the result of the Unfuture rather than the Unfuture itself. This allows cpu-bound @unsync functions to be chained together with other regular functions and IO-bound @unsync functions, as long as the result is pickleble.

Example

from dunsync import unsync
import asyncio
import time

@unsync()  # Will run in an asyncio event loop
async def download_data(url):
    await asyncio.sleep(1)
    return 'data'

@unsync(cpu_bound=True)  # Will run in a separate process
def process_data(data):
    time.sleep(1)
    return 'processed data'

@unsync()  # Will run in a separate thread
def store_processed_data(data):
    time.sleep(1)
    return 'Done'

tasks = [
    download_data(url).then(process_data).then(store_processed_data)
    for url in ['url1', 'url2', 'url3']
]

for task in tasks:
    print(task.result())

Replacing dunsync with unsync in the above example results in the error TypeError: cannot pickle '_asyncio.Task' object, since the Unfuture wraps other objects (either asyncio.Task, as in this example, or threading.Thread) which cannot be pickled in order to be passed to a separate process.

Installation

Using pip:

pip install dunsync

Using pipenv:

pipenv install dunsync

Using poetry:

poetry add dunsync

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

dunsync-0.1.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

dunsync-0.1.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

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