Play Python async

There are a few post about python asyncio introduced since Python 3.3. I played around and this is the note for myself.

Summary I evaluated thread and coroutine and find out the fact that the performace does not have a huge difference. Though, async is faster a little.(delimiter: I tested them with a really simple model. I do not say this is the ground truth.) Perhaps, the advantage of coroutine is chaining using yield from, which is easy to write compared with using queue and thread

Spec

Python async module was introduced in 3.3.

I'm not trying to reinvent the wheel. I'm trying to build a good one.

Guido van Rossum

import asyncio

Goals

Non goals

Coroutine, Futures and Tasks

Coroutines

Thread vs Coroutine

import time
import asyncio
import threading
import numpy as np


def fib(n):
    if n in (1, 2):
        return 1
    return fib(n - 1) + fib(n - 2)

@asyncio.coroutine
def co_fib(n):
    if n in (1, 2):
        return 1
    return fib(n - 1) + fib(n - 2)

def coroutine_test(n):
    tasks = [asyncio.ensure_future(co_fib(20)) for _ in range(n)]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))

def thread_test(n):
    threads = [threading.Thread(target=fib, args=(20,)) for _ in range(n)]
    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

def evaluate(n, m):
    r = []
    print("thread start")
    for _ in range(n):
        s = time.time()
        thread_test(m)
        process_time = time.time() - s
        r.append(process_time)
    print("thread average: ", np.average(r), " secs")
    print("thread distribution: ", np.var(r))

    r = []
    print("coroutine start")
    for _ in range(n):
        s = time.time()
        coroutine_test(m)
        process_time = time.time() - s
        r.append(process_time)
    print("coroutine average: ", np.average(r), " secs")
    print("coroutine distribution: ", np.var(r))


if __name__ == "__main__":
    evaluate(100, 1000)


Result

thread start
thread average:  2.26430218458  secs
thread distribution:  0.000516381104439
coroutine start
coroutine average:  2.17451543093  secs
coroutine distribution:  0.00377455202351

reference

http://www.slideshare.net/saghul/asyncio

Recommended Posts

Play Python async
Play with 2016-Python
Play youtube in python
python async / await curio
Python hand play (division)
Scraping using Python 3.5 async / await
Python hand play (two-dimensional list)
Python
[Python] Play with Discord's Webhook.
Play RocketChat with API / Python
Python hand play (argparse minimum code)
Python: How to use async with
Hide websockets async / await in Python3
Let's play with Excel with Python [Beginner]
[Python] Asynchronous request with async / await
kafka python
Play video with sound with python !! (tkinter / imageio)
Python Summary
Built-in python
Python comprehension
Python technique
Python hand play (calculated full of mordred)
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Install Pyaudio to play wave in python
Python service
python tips
Play audio files from Python with interrupts
python function ①
Python basics
Python memo
Play handwritten numbers with python Part 2 (identify)
ufo-> python (3)
Continuously play the first Python Sukusta MV
Python comprehension
Python hand play (descriptor calculation: serious version)
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Search and play YouTube videos in Python
Installing Python 3.4.3.
Python memo
Python iterative
Fractal to make and play with Python
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary