https://github.com/jczic/MicroWorkers
  
  
    A micro workers class that easily manages a pool of threads to optimise simultaneous jobs and jobs endings, for MicroPython (used on Pycom modules & ESP32) 
    https://github.com/jczic/MicroWorkers
  
concurency concurrency esp32 hc2 jobs lopy micropython multithread multithreading pool pycom queue thread threads wipy worker-queue worker-threads workers
        Last synced: 7 months ago 
        JSON representation
    
A micro workers class that easily manages a pool of threads to optimise simultaneous jobs and jobs endings, for MicroPython (used on Pycom modules & ESP32)
- Host: GitHub
 - URL: https://github.com/jczic/MicroWorkers
 - Owner: jczic
 - License: mit
 - Created: 2018-03-06T22:18:12.000Z (over 7 years ago)
 - Default Branch: master
 - Last Pushed: 2019-09-19T22:43:45.000Z (about 6 years ago)
 - Last Synced: 2025-04-03T04:41:21.704Z (7 months ago)
 - Topics: concurency, concurrency, esp32, hc2, jobs, lopy, micropython, multithread, multithreading, pool, pycom, queue, thread, threads, wipy, worker-queue, worker-threads, workers
 - Language: Python
 - Homepage: https://microworkers.hc2.fr
 - Size: 61.5 KB
 - Stars: 47
 - Watchers: 4
 - Forks: 12
 - Open Issues: 0
 - 
            Metadata Files:
            
- Readme: README.md
 - License: LICENSE.md
 
 
Awesome Lists containing this project
- awesome-micropython-lib - MicroWorkers - A micro workers class that easily manages a pool of threads (Threading)
 - awesome-micropython - MicroWorkers - A micro workers class that easily manages a pool of threads to optimise simultaneous jobs and jobs endings, for MicroPython (used on Pycom modules & ESP32). (Libraries / Threading)
 
README
          ### MicroWorkers is a class that easily manages a pool of threads to optimise simultaneous jobs and jobs endings, for MicroPython (used on ESP32 and [Pycom](http://www.pycom.io) modules)

Very easy to integrate and very light with one file only :
- `"microWorkers.py"`
Simple but effective :
- Use it to create a multitasking jobs container
- Add jobs without blocking your main code
- Be alerted by an event when a job has finished
### Using *microWorkers* class :
| Description  | Function |
| - | - |
| Constructor | `workers = MicroWorkers(workersCount, workersStackSize=None)` |
| Adds a job | `workers.AddJob(name, function, arg=None, onFinished=None)` |
| Description  | Property |
| - | - |
| Returns workers count | `workers.Count` |
| Returns emaining jobs count | `workers.JobsInQueue` |
| Returns jobs in process count  | `workers.JobsInProcess` |
| Returns `True` if is in working | `workers.IsWorking` |
### Simple example :
```python
from microWorkers import MicroWorkers
from time         import sleep
def sleepJob(jobName, jobArg) :
	sleep(10)
	return True
workers = MicroWorkers(workersCount=5)
for i in range(5) :
	workers.AddJob('Job %s' % i, sleepJob)
```
### Example of using multiple jobs (with finished event) :
```python
from microWorkers import MicroWorkers
from time         import sleep
print()
def jobA(jobName, jobArg) :
    sleep(1)
    return '%s:OK:1s' % jobName
def jobB(jobName, jobArg) :
    sleep(2)
    return '%s:OK:2s' % jobName
def jobC(jobName, jobArg) :
    sleep(3)
    return '%s:OK:3s' % jobName
def jobFinished(jobName, jobArg, jobResult) :
    print('Job %s finished (%s)' % (jobName, jobResult))
# workersStackSize must be greater than zero
# it can be None to use the default stack size
workers = MicroWorkers(workersCount=5, workersStackSize=10*1024)
for x in range(5) :
    workers.AddJob('JobA.%s' % x, jobA, arg=None, onFinished=jobFinished)
    workers.AddJob('JobB.%s' % x, jobB, arg=None, onFinished=jobFinished)
    workers.AddJob('JobC.%s' % x, jobC, arg=None, onFinished=jobFinished)
# Waiting end of all jobs,
while workers.IsWorking :
    sleep(0.100)
```
### By JC`zic for [HC²](https://www.hc2.fr) ;')
*Keep it simple, stupid* :+1: