Giter Club home page Giter Club logo

threaders's Introduction

threaders

logo
version open issues downloads code size

threaders is a small module to help write
clean threaded code using threading decorators
and minimize repeating copy-paste actions.

Install

Install and update using pip:

pip install -U threaders

Usage

from threaders import threaders
import time


@threaders.threader()
def function_to_be_threaded(x):
    """ :rtype: Thread """
    t = time.time()
    time.sleep(0.5*(x+0.1)/5+0.05)
    return time.time()-t


def main():
    # create threads
    threads = []
    for i in range(10):
        threads.append(function_to_be_threaded(i))

    # get first result
    print(threaders.get_first_result(threads))

    # kill threads
    t = time.time()

    for thread in threads:
        thread.join()
    print("all threads terminated: {}".format(time.time()-t))


if __name__ == "__main__":
    main()

Example with a thread pool:

from random import randrange
from time import sleep
import threading


delays = [randrange(1, 3) for i in range(50)]
print_lock = threading.Lock()


def wait_delay(i, d):
    with print_lock:
        print('{} sleeping for ({})sec'.format(i, d))
    sleep(d)


pool = threaders.ThreadPool(10)
for i, d in enumerate(delays):
    pool.put(wait_delay, i, d)
pool.join()

threaders's People

Contributors

yehonadav avatar

Stargazers

 avatar

Forkers

richlysakowski

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.