Alex Ng - ProjectThreading Library

1/2024 - Present

/images/dark.svg

Wraps around the python threading library and provides extra functionality

Project Image

Introduction

This is an Open Source Python threading library, it is built primarily to extend upon Python's built-in threading library.

Aims

This library aims to be:

  • Type-safe
  • Lightweight
  • Lastly... Your threading solution! ♡⸜(˶˃ ᵕ ˂˶)⸝♡

Background

I originally built this library as I was frustrated that python's built-in threading library does not have native support for returning values from threaded functions.

This is an example threaded function with Python's built-in library, there is no native way to retrieve the return value.

python
import threading def threadedTarget(): return 'this is my returned value' newThread = threading.Thread(target = threadedTarget) newThread.start() # Returns None newThread.join() # Returns None

Below is a "hacky" way to retrieve a value from a threading function.

python
import threading returnValue = None def threadedFunc(): global returnValue returnValue = 'hi' newThread = threading.Thread(target = threadedFunc) newThread.start() newThread.join() print(returnValue) # Returns 'hi'

However, returnValue is vulnerable to thread blocking and being owerwritten. For example if threadA and threadB wanted to replace returnValue, depending on which thread acts first could block the other thread from writing to the variable - and vice versa, it could be overwritten by either thread.

This behavior is usually not what developers want, and that is where thread comes in!

License

This project is published under the BSD 3-Clause "New" or "Revised" License.