Skip to main content

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

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.


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

customthread-0.1.2.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

CustomThread-0.1.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file customthread-0.1.2.tar.gz.

File metadata

  • Download URL: customthread-0.1.2.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for customthread-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7540eab0a76940eabfad64ec8b64149b622d142664ef9ade032774958f1e7c82
MD5 cef2d93e07a235d441d7bf24b3469eba
BLAKE2b-256 82652cb76075444d8c40eec7383ee2ef7295c668f19a0e818c1dbedec537536e

See more details on using hashes here.

File details

Details for the file CustomThread-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: CustomThread-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for CustomThread-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2dda265c05906321e80ff182641775b14d20082066ec28cd6d91a0c799f33c89
MD5 303bab628b816044d3c0ad2557acb67a
BLAKE2b-256 29aaf5027257349bd7dcc5ac6c1ff47af12db2ff2b4ee99a62cfdca8dac6b418

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page