Skip to main content

Async tools for Python

Project description

Async Tools for Python.

Threading

Threading is the most simple thing, but because of GIL it’s useless for computation. Only use when you want to parallelize the access to a blocking resource, e.g. network.

Parallel

Source: asynctools/threading/Parallel.py

Execute functions in parallel and collect results. Each function is executed in its own thread, all threads exit immediately.

Methods:

  • join(): Wait for all tasks to be finished, and return two lists:
    • A list of results
    • A list of exceptions

Example:

from asynctools.threading import Parallel

def request(url):
    # ... do request
    return data

# Execute
pll = Parallel(request)
for url in links:
    pll.job(url)  # Starts a new thread


# Wait for the results
results, errors = pll.join()

Pool

Source: asynctools/threading/Pool.py

Create a pool of threads and execute work in it. Useful if you do want to launch a limited number of long-living threads.

Methods:

  • join(): Wait for all tasks to be finished and return (results, errors) (same as with `Pool <#pool>`__)
  • close(): Terminate all threads.
  • __enter__, __exit__ context manager to be used with with statement

Example:

from asynctools.threading import Pool

def request(url):
    # ... do long request
    return data

# Make pool
pool = Pool(request, 5)

# Assign some job
for url in links:
    pll.job(url)  # Runs in a pool

# Wait for the results
results, errors = pll.join()

Project details


Release history Release notifications

History Node

0.1.2-2

History Node

0.1.2-1

History Node

0.1.2-0

History Node

0.1.0-0

This version
History Node

0.0.1-0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
asynctools-0.0.1-0.linux-x86_64.tar.gz (5.0 kB) Copy SHA256 hash SHA256 Dumb Binary any Jul 17, 2014
asynctools-0.0.1-0.tar.gz (4.4 kB) Copy SHA256 hash SHA256 Source None Jul 17, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page