Skip to main content

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

Installation

Para instalar o CustomThread via pip, execute:

pip install CustomThread

Depois, você pode importar e usar a classe CustomThread em seu projeto:

from CustomThread.customThread import CustomThread

Certifique-se de ter o Python 3.x instalado. Para mais detalhes, visite a página do PyPI.

Usage

from CustomThread.customThread 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 join method.
  • Exception Handling: Allows raising exceptions in the thread using the raise_exception method.
  • 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
  • ctypes module (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 is None).
  • 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 join method.

Contributors

License

This project is licensed under the MIT License.


Release files for CustomThread 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CustomThread 0.1.2
File Size Uploaded
customthread-0.1.2.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for CustomThread 0.1.2
File Interpreter ABI Platform
CustomThread-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.0 kB

Release files / customthread-0.1.2.tar.gz

Download URL customthread-0.1.2.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7540eab0a76940eabfad64ec8b64149b622d142664ef9ade032774958f1e7c82
BLAKE2b-256 checksum
How to use checksums
82652cb76075444d8c40eec7383ee2ef7295c668f19a0e818c1dbedec537536e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / CustomThread-0.1.2-py3-none-any.whl

Download URL CustomThread-0.1.2-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2dda265c05906321e80ff182641775b14d20082066ec28cd6d91a0c799f33c89
BLAKE2b-256 checksum
How to use checksums
29aaf5027257349bd7dcc5ac6c1ff47af12db2ff2b4ee99a62cfdca8dac6b418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page