DEV Community

Cover image for Threading Async Together
Joseph Boone
Joseph Boone

Posted on

Threading Async Together

Hello readers,

I built a proof-of-concept application I call TokenGate. It’s a high performance async/threaded event bus, with control mechanisms designed to be extremely minimalist.

The core concept is to produce parallelism in concurrent operations through async token gathering and coordinated threading workers.

Here's what "TokenGate" uses to thread an operation:

# -- Python 3.12 -- #
from token_system import task_token_guard
from operations_coordinator import OperationsCoordinator

# 1. Decorated standard synchronous function for threading
@task_token_guard(operation_type='string_ops', tags={'weight': 'light'})
def string_operation_task(task_data):
    # This function is now threaded
    return result

# 2. Starts the coordinator (through a running loop)
coordinator = OperationsCoordinator()
coordinator.start()

# 3. finally or an exception stops on close
coordinator.stop()
Enter fullscreen mode Exit fullscreen mode

Task tokens are generated by using a wrapped decorator.

Here's some test results on operations in a "release mechanism" that dispatches batches of mixed tasks incrementally:

CONCURRENCY BURST: Medium x8 | release 1464 (8 tasks)
======================================================================
  Submit spread (barrier jitter): 0.19ms
  Overall wall-clock:             0.009045s
  Min task duration:              0.007818s
  Max task duration:              0.008432s
  Mean task duration:             0.008148s
  Stdev (clustering indicator):   0.000218s

  Duration per task (tight clustering = true concurrency):
    Task 00: 0.007928s  
    Task 01: 0.008000s  
    Task 02: 0.008136s  
    Task 03: 0.008209s  
    Task 04: 0.008432s  
    Task 05: 0.008300s  
    Task 06: 0.008362s  
    Task 07: 0.007818s  

  Serial estimate (sum):  0.065186s
  Actual wall-clock:      0.009045s
  Concurrency ratio:      7.21x  (concurrent)

CONCURRENCY BURST [Medium x8 | release 1464] PASSED
======================================================================
CONCURRENCY WINDOW: Sustained mixed releases (30s)
======================================================================
  Releases:                       1484
  Total tasks:                    11872
  Overall wall-clock:             30.070291s
  Min task duration:              0.001157s
  Max task duration:              0.105874s
  Mean task duration:             0.014970s
  Stdev (clustering indicator):   0.025983s

  Serial estimate (sum):          177.728067s
  Actual wall-clock:              30.070291s
  Sustained concurrency ratio:    5.91x  (concurrent)

CONCURRENCY WINDOW [Sustained mixed releases (30s)] PASSED

CONCURRENCY SUITE COMPLETE.
Enter fullscreen mode Exit fullscreen mode

(Concurrency ratios of up to 7.21x were witnessed on an 8 core CPU with ~32 dynamic workers in ideal conditions, which is roughly 90% of the 8x concurrent operation ceiling.)

I've tested a wide variety of normally threaded operations with result delivery as expected.

It's still a just a proof, however I've used it in various side-projects with good results.

For anyone interested here's my project on GitHub (with proofs):

Repo link - https://github.com/TavariAgent/Py-TokenGate

Top comments (0)