Skip to main content

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, Wait

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

for _ in range(20):
    send_requests()

Wait()

Example Script:

This example script will take 55 seconds to run normally, but only 10 seconds when run asynchronously

import time
from asynchronizer import asynchronize, Wait, setWorkers

@asynchronize
def func(i):
    time.sleep(i)
    print i

for i in range(1,11):
    func(i)

Wait()

Things to keep in mind

  • The function Wait() is necessary. If Wait() is not present, your script will end without waiting for any unfinished functions to finish.

  • The function Wait() 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 use a custom number of workers, just add setWorkers(n) at the start of your script, with n being the number of concurrent greenlet threads you want. 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).

Release files for asynchronizer 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asynchronizer 0.2.4
File Size Uploaded
asynchronizer-0.2.4.tar.gz 3.6 kB Details

Release files / asynchronizer-0.2.4.tar.gz

Download URL asynchronizer-0.2.4.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
928a51092e695a9b45cbebed06a7af2a67cdec0afb57556c83988a456d82fee8
BLAKE2b-256 checksum
How to use checksums
3edccf0cd9576b3b5e9f4b1976f1770574fd506d548d23b20399048e4d5e3288
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.4 This release

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page