Package: lox
Threading and multiprocessing made easy.Free software: Apache-2.0 licenseDocumentation:https://lox.readthedocs.io.Python >=3.6Loxprovides decorators and synchronization primitives to quickly add
concurrency to your projects.Installationpip3 install –user loxFeaturesMultithreading: Powerful, intuitive multithreading in just 2 additional lines of code.Multiprocessing: Truly parallel function execution with the same interface asmultithreading.Synchronization: Advanced thread synchronization, communication, and resource management tools.TodosAll objects exceptlox.processare for threads. These will eventually be multiprocess friendly.UsageEasy Multithreading>>> import lox
>>>
>>> @lox.thread(4) # Will operate with a maximum of 4 threads
... def foo(x,y):
...     return x*y
>>> foo(3,4) # normal function calls still work
12
>>> for i in range(5):
...     foo.scatter(i, i+1)
-ignore-
>>> # foo is currently being executed in 4 threads
>>> results = foo.gather() # block until results are ready
>>> print(results) # Results are in the same order as scatter() calls
[0, 2, 6, 12, 20]Or, for example, if you aren’t allowed to directly decorate the function you
would like multithreaded/multiprocessed, you can just directly invoke the
decorator:>>># Lets say we don't have direct access to this function...deffoo(x,y):...returnx*y...>>>>>>defmy_func():...foo_threaded=lox.thread(foo)...foriinrange(5):...foo_threaded.scatter(i,i+1)...results=foo_threaded.gather()...# foo is currently being executed in default 50 thread executor pool...returnresults...This also makes it easier to dynamically control the number of
thread/processes in the executor pool. The syntax is a little weird, but
this is just explicitly invoking a decorator that has optional arguments:>>># Set the number of executer threads to 10>>>foo_threaded=lox.thread(10)(foo)Easy Multiprocessing>>>importlox>>>>>>@lox.process(4)# Will operate with a pool of 4 processes...deffoo(x,y):...returnx*y...>>>foo(3,4)# normal function calls still work12>>>foriinrange(5):...foo.scatter(i,i+1)...-ignore->>># foo is currently being executed in 4 processes>>>results=foo.gather()# block until results are ready>>>print(results)# Results are in the same order as scatter() calls[0, 2, 6, 12, 20]Progress Bar Support (tqdm)>>>importlox>>>fromrandomimportrandom>>>fromtimeimportsleep>>>>>>@lox.thread(2)...deffoo(multiplier):...sleep(multiplier*random())...>>>foriinrange(10):>>>foo.scatter(i)>>>results=foo.gather(tqdm=True)90%|████████████████████████████████▌        | 9/10 [00:03<00:00,  1.32it/s]
100%|███████████████████████████████████████| 10/10 [00:06<00:00,  1.46s/it]History0.10.0 (2021-12-18)Remove dependency pinning.Allow@lox.thread(0). This will executescattercalls in parent thread.
Useful for debugging breakpoints in parallelized code.0.9.0 (2020-11-25)tqdmsupport onlox.process.gather. See v0.8.0 release notes for usage.0.8.0 (2020-11-25)tqdmsupport onlox.thread.gather* Can be a bool:>>> my_func.gather(tqdm=True)Can be atqdmobject:>>> from tqdm import tqdm
>>> pbar = tqdm(total=100)
>>> for _ in range(100):
>>>     my_func.scatter()
>>> my_func.gather(tqdm=pbar)0.7.0 (2020-07-20)Complete rework of workers
+ Fix memory leaksDrop support for python3.5Drop support for chaining in favor of simpler codebase0.6.3 (2019-07-30)Alternative fix for 0.6.2.0.6.2 (2019-07-21)Update dependenciesFix garbage-collecting exclusiviity0.6.1 (2019-07-21)Fix memory leak inlox.process.0.6.0 (2019-07-21)lox.Announcementsubscribe()calls now return anotherAnnouncementobject that behaves like a queue instead of an actual queue. Allows for
many-queue-to-many-queue communications.New Object:lox.Funnel. allows for waiting on many queues for a complete
set of inputs indicated by a job ID.0.5.0 (2019-07-01)New Object:lox.Announcement. Allows a one-to-many thread queue with
backlog support so that late subscribers can still get all (or most recent)
announcements before they subscribed.New Feature:lox.threadscattercalls can now be chained together.scatternow returns anintsubclass that contains metadata to allow
chaining. Each scatter call can have a maximum of 1 previousscatterresult.Documentation updates, theming, and logos0.4.3 (2019-06-24)Garbage collect cached decorated object methods0.4.2 (2019-06-23)Fixed multiple instances and successive scatter and gather calls to wrapped methods0.4.1 (2019-06-23)Fixed broken workers and unit tests for workers0.4.0 (2019-06-22)Semi-breaking change:lox.threadandlox.processnow automatically pass
the object instance when decorating a method.0.3.4 (2019-06-20)Print traceback in red when a thread crashes0.3.3 (2019-06-19)Fix bug where thread in scatter of lox.thread double releases on empty queue0.3.2 (2019-06-17)Fix manifest for installation from wheel0.3.1 (2019-06-17)Fix package on pypi0.3.0 (2019-06-01)Multiprocessing decorator.lox.poolrenamed tolox.threadSubstantial pytest bug fixesDocumentation examplestimeout for RWLock0.2.1 (2019-05-25)Fix IndexSemaphore context manager0.2.0 (2019-05-24)Added QLockDocumentation syntax fixes0.1.1 (2019-05-24)CICD test0.1.0 (2019-05-24)First release on PyPI.