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
Thread safe operations: http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm
Changelog
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pythreadworker-0.4.0.zip
.
File metadata
- Download URL: pythreadworker-0.4.0.zip
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a72f806373b51e0c4906fc4089a8b16c0ae8c62bbc19778f26e6c48dbe4c87f |
|
MD5 | c8ecacc448a826bdfe3c6e0f41047333 |
|
BLAKE2b-256 | 1c78932c439504a169d1f587a42dca4ff6ae5e977aa5b88134a9a48175c8f835 |
File details
Details for the file pythreadworker-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: pythreadworker-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77c24e21cc9f04523961df7064a44d2b316fa1e7cce06c4fd63d85dd4bc34618 |
|
MD5 | 5937ea76fdd394e61e3384f7ddd16f16 |
|
BLAKE2b-256 | e992a623d2fd0b0370d98e18f7d2db1f41fd7583316f644fba4bd632af07dab3 |