AsyncTools
Async Tools for Python.
Table of Contents
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.
Async
Source: asynctools/threading/Async.py
Decorator for functions that should be run in a separate thread.
When the function is called, it returns a threading.Event.
from asynctools.threading import Async
@Async
def request(url):
# ... do request
request('http://example.com') # Async request
request('http://example.com').wait() # wait for it to complete
If you want to wait for multiple threads to complete, see next chapters.
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:
-
__call__(*args, **kwargs): Add a job. Call theParallelobject so it calls the worker function with the same arguments -
map(jobs): Convenience method to call the worker for every argument -
first(timeout=None): Wait for a single result to be available, with an optional timeout in seconds. The result is returned as soon as it's ready. If all threads fail with an error --Noneis returned. -
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(url) # Starts a new thread
# Wait for the results
results, errors = pll.join()
Since the request method takes just one argument, this can be chained:
results, errors = Parallel(request).map(links).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 are same with Parallel, with some additions:
__call__(*args, **kwargs)map(jobs)first(timeout=None)close(): Terminate all threads. The pool is no more usable when closed.__enter__,__exit__context manager to be used withwithstatement
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(url) # Runs in a pool
# Wait for the results
results, errors = pll.join()
Metadata
Release files for asynctools 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asynctools-0.1.3.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asynctools-0.1.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 12.6 kB
Release files / asynctools-0.1.3.tar.gz
| Download URL | asynctools-0.1.3.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
77fffc74361a120d2e532fa4d5b7f7fbdc15a94edf3501cdc0402ec305e78de2
|
|
BLAKE2b-256 checksum How to use checksums |
ee8a6bc92af3b1cc02fac837ad7bf71448949d99fcd971ab42e8196aead3d2d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
|
Release files / asynctools-0.1.3-py2.py3-none-any.whl
| Download URL | asynctools-0.1.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
1c457795cee369b23c6b53db536963cc9ae0e2a4b2a9bb1cc2e27f4f4929a83d
|
|
BLAKE2b-256 checksum How to use checksums |
37a82680bd7963d18390d7541909a83508e0b4d5c004359029d3d9cc730aeb0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7
|