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 and restart thread.

  • Create child thread.

  • Create async task.

  • Communicate between threads with events.

  • Use channel to broadcast event.

Install

pip install pythreadworker

Usage

Basic

#! python3

from worker import Worker, sleep

count = None

def increaser():
        global count
        count = 1
        while True:
                print(count)
                count += 1
                sleep(1)

thread = Worker(increaser)

while True:
        command = input("input command: ")

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

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

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

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

        if command == "exit":
                thread.stop()
                break

Async task

#! python3

from worker import Async, sleep

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

async = Async(long_work, 5)

# Do other stuff here...

print(async.get())

Listen to event

#! python3

from worker import Worker

def create_printer():
        thread = Worker()

        @thread.listen("PRINT")
        def _(event):
                print(event.data)

        return thread.start()

thread = create_printer()
thread.fire("PRINT", "Hello thread!")
thread.stop()

Subscribe to channel

#! python3

from worker import Worker, Channel

def create_worker():
        thread = Worker()
        @thread.listen("PRINT")
        def _(event):
                print(event.data)
        channel.sub(thread)
        return thread.start()

channel = Channel()
thread = create_worker()
channel.pub("PRINT", "Hello channel!")
thread.stop()

Child thread and bubble/broadcast

#! python3

from worker import Worker, sleep

def create_worker(name, parent):
        thread = Worker(parent=parent)
        @thread.listen("HELLO")
        def _(event):
                print(name)
        return thread.start()

parent = create_worker("parent", None)
child = create_worker("child", parent)
grand = create_worker("grand", child)

# broadcast/bubble is happened in main thread. It doesn't gaurantee the execution order of listeners.
parent.fire("HELLO", broadcast=True)
sleep(1)
grand.fire("HELLO", bubble=True)
sleep(1)

# stop a thread will cause its children to stop
parent.stop()

Notes

Changelog

  • 0.5.1 (Apr 22, 2016)

    • Use float in sleep function.

  • 0.5.0 (Apr 22, 2016)

    • Add sync.

  • 0.4.0 (Apr 20, 2016)

    • Interface completely changed

    • Drop Message.put, .get

    • Drop UserWorker

    • Drop Worker.create_child. Use parent option in constructor instead.

    • Drop global_cleanup

    • Add sleep

    • Add current

    • Add Channel

    • Add Listener.priority

    • Add daemon option to Worker

    • Worker.cleanup –> Worker.update

    • Worker.message –> Worker.fire

    • Worker.wait_message –> Worker.wait_event

    • Worker.message_loop –> Worker.wait_forever

  • 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.5.1.zip (10.8 kB view details)

Uploaded Source

Built Distribution

pythreadworker-0.5.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pythreadworker-0.5.1.zip.

File metadata

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

File hashes

Hashes for pythreadworker-0.5.1.zip
Algorithm Hash digest
SHA256 c9e038f24d713dd75456b55396a98c8146832e1d5f210d4f91147a09a8801f2b
MD5 040f3a8e5922ad28317d9dfb26d8b997
BLAKE2b-256 18f92f6b03fd1810ad659e7b477248610b1e5357971b077e9900ffd185c44205

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pythreadworker-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9cd7a416f831c52e166ecfeb70bbe516ab65c564fbd7670db8876e5e0c7b528
MD5 9754111197a8e78f3b6d50c2b6defb89
BLAKE2b-256 0424d6ae65f46e9d2699417fc2eace040deab086c2efdd442585d8b27d008957

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