Skip to content

ziberna/threaded

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

@threaded

@threaded is a collection of Python decorators for easy threading. They come in handy when you have a function that you want to run in a separate thread, but you don't want to (or don't need to) deal with threading objects.

Decorators

threaded.inherit

Inherits daemon property from the current thread.

threaded.daemon

The resulting thread is a daemon thread.

threaded.nondaemon

The resulting thread is a non-daemon thread.

threaded.opposite

The thread's daemon property is opposite than that of the current thread.

Special keyword arguments

_delay

Delays function call for a float number of seconds. Pass to the function call as _delay=seconds where seconds is a floating point number. This must be combined with one of the decorators above to work.

Example

import threaded
import time


# Replace with threaded.daemon and run again. Notice any changes?
@threaded.inherit
def print_odd_numbers(limit, interval):
    number = 1
    while number < limit:
        print('\t', number)
        number += 2
        time.sleep(interval)

def print_even_numbers(limit, interval):
    number = 0
    while number < limit:
        print(number)
        number += 2
        time.sleep(interval)

print('MAIN\t', '2ND THREAD')
print_even_numbers(30, 0.25)  # will be run in MAIN thread
print_odd_numbers(30, 0.5, _delay=1.5)  # will be run in 2ND thread

About

a Python decorator for easy threading

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages