Giter Club home page Giter Club logo

thredo's Introduction

thredo

Thredo is threads on async. For the brave. Or the foolish. Only time will tell.

High Level Overview (The Big Idea)

Consider the following thread program involving a worker, a producer, and queue::

import threading
import queue
import time

def worker(q):
    while True:
        item = q.get()
        if item is None:
            break
        print('Got:', item)

def main():
    q = queue.Queue()
    t = threading.Thread(target=worker, args=(q,))
    t.start()
    for n in range(10):
        q.put(n)
        time.sleep(1)
    q.put(None)
    t.join()

main()

In this code, there are blocking operations such as q.get() and time.sleep(). This blocking is ultimately handled by the host operating system. Because of that, it is very difficult for Python to do anything related to the actual control or scheduling of threads. Once blocked, a thread stays blocked forever or until some event occurs that causes it to unblock.

Thredo re-envisions threads by redirecting all blocking operations to an async library. The code looks mostly the same except that you use the thredo module. For example:

import thredo

def worker(q):
    while True:
        item = q.get()
        if item is None:
            break
        print('Got:', item)

def main():
    q = thredo.Queue()
    t = thredo.spawn(worker, q)
    for n in range(10):
        q.put(n)
        thredo.sleep(1)
    q.put(None)
    t.join()

thredo.run(main)

The main reason you'd use thredo however is that it gives you extra features such as thread groups, cancellation, and more. For example, here's a more advanced version of the above code::

import thredo

def worker(q):
    try:
        while True:
            item = q.get()
            print('Got:', item)
            q.task_done()
    except thredo.ThreadCancelled:
        print('Worker cancelled')

def main():
    q = thredo.Queue()
    with thredo.ThreadGroup(wait=None) as workers:
        for n in range(4):
            workers.spawn(worker, q)

        for n in range(10):
            q.put(n)
            thredo.sleep(1)

        workers.join()    

thredo.run(main)

Examples

The examples directory contains more examples of using thredo. The examples/euro directory contains coding samples from the EuroPython 2018 talk.

thredo's People

Contributors

dabeaz avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.