Skip to main content

A simple module to make functions asynchronous

Project description

Asynchronizer is simple module that can be used to run multiple functions asynchronously. To convert a function, you just need to add a decorator @asynchronize to the function. This project is still in development, so report any bugs here. For examples, see the examples folder

Requirements

  • python 2.x or python 3.x

Installation

Asynchronizer can be installed using pip:

pip install asynchronizer

How to use

Basic use

Suppose you have a function like this:

import requests

def send_requests():
    r = requests.get('http://httpbin.org/get')
    print r.status_code

for _ in range(20):
    send_requests()

You can modify it like this to make it asynchronous:

import requests
from asynchronizer import asynchronize, startPool

@asynchronize
def send_requests():
    r = requests.get('http://httpbin.org/get')
    print r.status_code

for _ in range(20):
    send_requests()

startPool()

Things to keep in mind

  • The function startPool() is necessary. If startPool() is not present, none of the asynchronous functions will run.

  • The function startPool() is also a blocking function, meaning that the execution of your script will pause here till all the async functions called before this are finished. This is why it should usually be added at the end of your script

  • The decorated functions are async to each other, but the code inside the functions is synchronous, which means this is wrong:

    # wrong way
    @asynchronize
    def send_requests():
        for _ in range(20):
            r = requests.get('http://httpbin.org/get')
    
    send_requests()

    and this is the correct way:

    # correct way
    @asynchronize
    def send_requests():
        r = requests.get('http://httpbin.org/get')
    
    for _ in range(20):
        send_requests()
  • Instead of returning values from your functions, send them to a callback. For example:

    @asynchronize
    def send_requests():
        r = requests.get('http://httpbin.org/get')
        parse(r.text)
        # instead of return r.text

Advanced use

  • If you want to modify how many functions should be called concurrently, just add setWorkers(n) at the start of your script, with n being the number of concurrent threads. Default is 32.

  • To assign priority to a specific function call, add priority=n to the parameters of the function call, with n being the priority you want to set. For Example: func(param1,param2,param3,priority=2)

Contributing

If you want to contribute to this project, feel free to send a Pull Request to Github

To report any bugs or request new features, head over to the Issues page

License

Licensed under The MIT License (MIT).

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

asynchronizer-0.1.0a1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

asynchronizer-0.1.0a1-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file asynchronizer-0.1.0a1.tar.gz.

File metadata

File hashes

Hashes for asynchronizer-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 8ae238a02d69203530ea990a15a16e465181e7b193b5536af9e8dc749468ca20
MD5 8e45968af93f0caa8a6d3a33b929f6f4
BLAKE2b-256 4cff9f6b473a34562143d271815784b4ec20487ed7d82c7d7885fa22cb2849ad

See more details on using hashes here.

File details

Details for the file asynchronizer-0.1.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for asynchronizer-0.1.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a144a837694145d3b43060af871df34bb800ab02688d0119f6be4327352da860
MD5 78386b9294fa9fc2b04d631f24d6665e
BLAKE2b-256 922548b5dca77f116358be218a7e72c532fcb21640fd25b7dc7c31adde9da068

See more details on using hashes here.

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