Skip to main content

A threading library written in python

Project description

A threading library written in python. Help you build threaded app.

This module was originally included in ComicCrawler.

Features

  • Pause, resume, stop, restart thread.

  • Create child thread.

  • Create async tasks.

  • Communicate between threads with Message.

Install

pip install pythreadworker

Usage

Use function as target:

#! python3

from worker import Worker

count = 1

def increaser(thread):
        global count
        while True:
                count += 1
                thread.wait(1)

ic = Worker(increaser)
ic.start()

while True:
        command = input("print|pause|resume|stop|exit: ")

        if command == "print":
                print(count)

        if command == "pause":
                ic.pause()

        if command == "resume":
                ic.resume()

        if command == "stop":
                ic.stop()

        if command == "exit":
                ic.stop()
                ic.join()
                break

Parent, child thread:

#! python3

from worker import Worker

p_thread = None
c_thread = None

def parent(thread):
        global p_thread, c_thread

        p_thread = thread
        c_thread = thread.create_child(child)
        c_thread.start()

        thread.message_loop()

def child(thread):
        thread.message_loop()

Worker(parent).start()

while True:
        command = input("print|stop|exit: ")

        if command == "print":
                print("p_thread.is_running(): {}\nc_thread.is_running(): {}".format(
                        p_thread.is_running(),
                        c_thread.is_running()
                ))

        if command == "stop":
                p_thread.stop()

        if command == "exit":
                p_thread.stop()
                p_thread.join()
                break

Async task:

#! python3

from worker import Worker
from time import sleep

def long_work(t):
        sleep(t)
        return "Finished in {} second(s)".format(t)

lw_thread = Worker.async(long_work, 5)

# Do other stuff here...

print(lw_thread.get())

Async + parent/child:

#! python3

from worker import Worker
from time import sleep

p_thread = None
c_thread = None

def long_work(t):
        sleep(t)
        return "Finished in {} second(s)".format(t)

def parent(thread):
        global p_thread, c_thread

        p_thread = thread
        async = thread.async(long_work, 5)
        c_thread = async.thread

        # Do other stuff here...

        print(thread.await(async))

Worker(parent).start()

while True:
        command = input("print|stop|exit: ")

        if command == "print":
                print("p_thread.is_running(): {}\nc_thread.is_running(): {}".format(
                        p_thread.is_running(),
                        c_thread.is_running()
                ))

        if command == "stop":
                p_thread.stop()

        if command == "exit":
                p_thread.stop()
                p_thread.join()
                break

Message:

#! python3

from worker import Worker

def work(thread):
        @thread.listen("hello")
        def _():
                return "world!"

        @thread.listen("ok")
        def _():
                return "cool"

        thread.message_loop()

w_thread = Worker(work)
w_thread.start()

while True:
        command = input("<message>|exit: ")

        if command == "exit":
                w_thread.stop()
                w_thread.join()
                break

        else:
                message = w_thread.message(command)

                # Do other stuff here...

                print(message.get())

Message + parent/child:

#! python3

from worker import Worker
from time import sleep

def odd_man(thread):

        @thread.listen("hey")
        def _(number):
                print(number)
                sleep(1)
                thread.bubble("hey", number + 1)

        thread.message_loop()

def even_man(thread):

        @thread.listen("hey")
        def _(number):
                print(number)
                sleep(1)
                thread.broadcast("hey", number + 1)

        od_thread = thread.create_child(odd_man)
        od_thread.start()

        thread.message("hey", 0)

        thread.message_loop()

w_thread = Worker(even_man)

while True:
        command = input("start|stop|exit: ")

        if command == "start":
                w_thread.start()

        if command == "stop":
                w_thread.stop()

        if command == "exit":
                w_thread.stop()
                w_thread.join()
                break

Clean up threads on exit:

#! python3

from worker import Worker, global_cleanup

def loop(thread):
        thread.message_loop()

# if you doesn't hold the reference, the thread become daemon thread.
Worker(loop).start()

# pyWorker provide a cleanup function to stop all threads.
global_cleanup()

Known issues

  • If there is an error in worker.sync, the error message will be printed twice, once in the child thread and once in the parent.

Changelog

  • Version 0.3.0 (Jun 14, 2015)
    • Catch BaseException.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythreadworker-0.3.0.zip (10.7 kB view details)

Uploaded Source

Built Distribution

pythreadworker-0.3.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file pythreadworker-0.3.0.zip.

File metadata

  • Download URL: pythreadworker-0.3.0.zip
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pythreadworker-0.3.0.zip
Algorithm Hash digest
SHA256 fb8b836db25b17ae7ac4e5a2d0e1637a9608ea0b801a37cfc635b8d555120835
MD5 fdb4e176e43f02f118311102bd7df584
BLAKE2b-256 0deaa4eda8b63cce22b47d2b0d4cc992fcd0a817445762e568128cd7641f8c9c

See more details on using hashes here.

File details

Details for the file pythreadworker-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pythreadworker-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f61ae63e14ad7c52fbfdbb8408c832efaa2cc532cc8dd7b529abde1c0e4d07f4
MD5 5ad39d93a5e1843e589e3a5c11b97f45
BLAKE2b-256 49b39eb114cee7196804f7d8ca61e6d7f60fb116728c999e217c85e317e3195b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page