A custom threading class in Python that extends the standard threading.Thread.
Project description
CustomThread
Introduction
CustomThread is a custom threading class in Python that extends the functionality of the standard threading.Thread class. It provides additional features such as returning the result of the target function, raising exceptions in the thread, and more control over the thread lifecycle.
Table of Contents
- Introduction
- Installation
- Usage
- Features
- Dependencies
- Configuration
- Documentation
- Examples
- Troubleshooting
- Contributors
- License
Installation
To use CustomThread, simply copy the class definition into your project. Ensure you have Python installed (version 3.x is recommended).
Usage
from custom_thread import CustomThread
def example_function(param1, param2):
print(f"Parameters are {param1} and {param2}")
return param1 + param2
# Create a CustomThread instance
thread = CustomThread(target=example_function, args=(5, 10))
# Start the thread
thread.start()
# Join the thread and get the return value
result = thread.join()
print(f"Result from thread: {result}")
Features
- Return Value: The thread can return a value from the target function, which can be accessed by calling the
joinmethod. - Exception Handling: Allows raising exceptions in the thread using the
raise_exceptionmethod. - Thread Identification: Provides a method to get the thread ID with
get_id. - Stop Event: Uses a threading event to indicate when the thread should stop.
- Daemon Thread Support: Can be set as a daemon thread during initialization.
Dependencies
- Python 3.x
ctypesmodule (included in the standard library)
Configuration
No specific configuration is required. The class can be directly used by creating instances of CustomThread.
Documentation
Class: CustomThread
__init__(self, group=None, target=None, name=None, args=(), kwargs={}, daemon=None, verbose=None)
Initializes a new CustomThread instance.
- group: Thread group (default is
None). - target: Target function to call when the thread starts.
- name: Thread name (default is
None). - args: Arguments to pass to the target function (default is an empty tuple).
- kwargs: Keyword arguments to pass to the target function (default is an empty dictionary).
- daemon: If
True, the thread will run as a daemon (default isNone). - verbose: Verbosity level (not used in the current implementation).
run(self)
Executes the target function with the given arguments and keyword arguments, storing the return value.
join(self, *args, **kwargs)
Waits for the thread to finish and returns the result of the target function.
get_id(self)
Returns the ID of the respective thread.
raise_exception(self)
Raises a SystemExit exception in the thread to stop its execution. If the exception cannot be raised, an error message will be printed.
Examples
See the Usage section for a basic example.
Troubleshooting
- Exception Raise Failure: If raising an exception in the thread fails, an error message will be printed. Make sure the thread is running when trying to raise an exception.
- Return Value: Ensure the target function returns a value if you intend to capture it using the
joinmethod.
Contributors
License
This project is licensed under the MIT License.
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 CustomThread-0.1.0.tar.gz.
File metadata
- Download URL: CustomThread-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e0c2fde2f85adbce26d0adcb5cfc83686192fd005a63e0f9170c13b6f18f008
|
|
| MD5 |
7755a894a3aad937c53569c60083f601
|
|
| BLAKE2b-256 |
32fa18d02e05e0b290a4652ce8573af2094dcdf912981bcf15399130bb690cd3
|
File details
Details for the file CustomThread-0.1.0-py3-none-any.whl.
File metadata
- Download URL: CustomThread-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccbeaae5a505d6be3ab9b40ac74dbedc77d52ecf277c036dcefcb745acd2b3cc
|
|
| MD5 |
34a90ad2e2a433de5485fe0687bec92c
|
|
| BLAKE2b-256 |
f3637992004929c016efb67fdb6052e9d9ae47519f564881ffb59d7ecd64d3dc
|