Python Threads - from Dreadful to Threadful
Project description
Threadful
Python Threads - from Dreadful to ThreadfulInstallation
pip install threadful
Usage
Example 1: Basic usage of @thread
from threadful import thread
@thread # with or without ()
def some_function():
time.sleep(10)
return " done "
# when ready, it will call these callback functions.
some_function().then(lambda result: result.strip()).then(lambda result: print(result)) # prints: "done"
promise = some_function() # ThreadWithResult[str] object
promise.result() # Err(None)
time.sleep(15) # after the thread is done:
promise.result() # Ok(" done ")
# alternative to sleep:
result = promise.join() # " done " if success, raises if the thread raised an exception
What's happening:
- The
@threaddecorator wrapssome_functionto make it run in a separate thread when invoked. - Calling
some_function()doesn't start the thread immediately. Instead, it returns aThreadWithResultobject, allowing you to attach callbacks using.then()or.catch()before the thread starts. - The thread starts running when you begin checking for the result (
result,join) or explicitly start it. - The
.then()chain demonstrates how to process the result when the thread completes. - The
promise.result()method gives access to the result (OkorErr) if available. - The
.join()method blocks until the thread finishes and directly returns the result or raises any exception from the thread.
Example 2: Handling exceptions in threads
@thread()
def raises() -> str:
raise ValueError()
promise = raises().catch(lambda err: TypeError())
promise.join() # raises TypeError
promise.result() # Err(TypeError)
promise = raises().catch(lambda err: "Something went wrong")
promise.join() # returns the string "Something went wrong"
What's happening:
- The
@threaddecorator is used onraises(), which deliberately raises aValueError. - The first
catch()replaces theValueErrorwith aTypeError, so callingpromise.join()raises the newTypeError. - The second
catch()provides a fallback string"Something went wrong". When the thread completes, callingjoin()returns this string instead of raising an exception. - This mechanism allows you to gracefully handle errors in the thread without crashing your program.
Example: Animating a Function with Different Options
from threadful import thread, animate
import time
@thread
def wait(duration: int):
time.sleep(duration)
return f"Waited for {duration} seconds"
# Example 1: Basic animation with static text
result = animate(wait(3), text="Waiting...") # Output: "Waited for 3 seconds"
# Example 2: Threaded animation (non-blocking)
thread_result = animate(wait(3), text="Running asynchronously...", threaded=True)
# Animation running in the background...
# you can do other things in the main thread here
thread_result.join() # Get the result (str) after the thread completes
# Example 3: Dynamic text animation
animate(wait(3), text=lambda: f"Current time: {time.strftime('%H:%M:%S')}")
# Animation with dynamic text complete!
What's Happening:
- Basic Animation: Runs
waitwith a loading message ("Waiting...") and returns the result after completion. - Threaded Animation: Runs both
waitand the animation in the background. Use.join()to get the result. - Dynamic Text: Updates the animation text dynamically using a callback (e.g., current time).
These examples show how to use animate for different scenarios.
Important Note:
A thread doesn't start running immediately when you invoke the decorated function (e.g., some_function()). This delay allows you to attach callbacks (then, catch) before the thread begins execution. The thread starts:
- When you check for its result (
result). - When you block for its completion (
join). - In the background if you explicitly call
.start().
License
threadful is distributed under the terms of the MIT license.
Changelog
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file threadful-0.4.0.tar.gz.
File metadata
- Download URL: threadful-0.4.0.tar.gz
- Upload date:
- Size: 46.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b6ecd171bcf5ce796da9753621ba127c99d72addb81c883e7012323550b7746
|
|
| MD5 |
24d7deda30b83751b3ec072ef1d30203
|
|
| BLAKE2b-256 |
a616dfe2bfb10bcd9beb3110a1cba417f4c264da68da915cb44c156b51c28414
|
File details
Details for the file threadful-0.4.0-py3-none-any.whl.
File metadata
- Download URL: threadful-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d3726d54988a655c56b2f7154a0f84827cab7334df5f186223e294148008928
|
|
| MD5 |
c577e3bc092fd4b99191bf2320e3b4a8
|
|
| BLAKE2b-256 |
b3d3012bdaa6d9c46c21d246afa5e3999168749b45e3f6836cba1a6f973ccb6b
|