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 import CustomThread

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

Usage

from 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.1.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.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file CustomThread-0.1.1.tar.gz.

File metadata

  • Download URL: CustomThread-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 f4bd4eb669f2e609cc066a4ff4ddeece86fa38f2adc14c9b75b98af5ead327aa
MD5 d67c45b7a230fabf5354684d759b032a
BLAKE2b-256 a0702056a36243592217f370e602a3e5f0ef776ede2beaeac6686a246a0c0eb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: CustomThread-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ca4e6e0023837fb6c52ab24d41716e45133b069499e43c789f324582779e3f0c
MD5 fbf3260b803dc349a78960a8f2decf37
BLAKE2b-256 14d5577b63d734aec7579033d1bf0411c757dcb245c274fd1bab207dd6d8cbee

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