/easy-thread

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

easy-thread

By default easyThread will create 10 threads and 10 jobs to perform. The default parameters easyThread will use are:

  • jobList = range(10)
  • nThread = 10

jobList - A list of things you want to pass the function which is threaded. It could be anything. As a silly example below, we use the jobList to print values in the range 0-9. If you choose not to use the elements within jobList for use directly in EasyThread.theThread() that is okay as well. The main thing to keep in mind is that jobList is the maxmimum amount of times your function is repeated. For situations where this is not ideal, the option to pass a queue object to jobList has been added allowing the user to feed the jobList as time passes.

nThread - This is an integer representing the threads available for your use.

EasyThread.theThread()

This is the function which you will create. As there was no purpose to create this object in the file, the code itself does not contain it. You the user must define what theThread() does. An example of how to do so is below.

Example usage #1 -- EasyThread Method modification

Add our function as a method to EasyThread. This has the benefit of allowing our function access to the Class via the function becoming a Method of the Class.

    from easyThread import EasyThread

    ## Create a function defining what you want your jobs to do
    ## Use of the work object is not required, but is provided if wanted
    def exampleThread1(self, work):
        print (work)
        print (self.nThread)

    ## Add our function to EasyThread
    EasyThread.theThread = exampleThread1

    ## Instantiate using #s other than defaults
    et = EasyThread(jobList = [1,2,3,4,5], nThread = 5)

    ## Start the work
    et.easyLaunch()

Example usage #2 -- EasyThread Parameterization

Pass our function as a parameter to EasyThread. Does not allow our function access to the Class:

    from easyThread import EasyThread

    ## Create a function defining what you want your jobs to do
    ## Use of the work object is not required, but is provided if wanted
    def exampleThread2(work):
        print(work)

    ## Instantiate with our function using the default parameters
    et = EasyThread(exampleThread2)

    ## Start the work
    et.easyLaunch()

Example usage #3 -- Backgrounder Method modification

Add our function as a method to Backgrounder. This has the benefit of allowing our function access to the Class via the function becoming a Method of the Class.

    from easyThread import Backgrounder

    ## Create a function defining what you want your jobs to do
    ## Use of the work object is not required, but is provided if wanted
    def exampleThread3(self):
        print ('hello world')

    ## Add our function to Backgrounder
    Backgrounder.theThread = exampleThread3

    ## Instantiate using #s other than defaults
    bg = Backgrounder()

    ## Start the work
    bg.easyLaunch()

Example usage #4 -- Combination approach

Combine Backgrounder and EasyThread with a Class and using a queue as the input for the jobList

import time
from easyThread import Backgrounder
from easyThread import EasyThread
from queue import Queue

class OurClass(object):
    def foo(self, work):
        while True:
            print(self.q.get())
            self.q.task_done()

## Instantiate and create a FIFO queue
oc = OurClass()
oc.q = Queue()

## Add our function to EasyThread
EasyThread.theThread = oc.foo
et = EasyThread(jobList = oc.q, nThread = 5)

## Start the work
def foo(self):
    et.easyLaunch()
Backgrounder.theThread = foo
bg = Backgrounder()
bg.easyLaunch()

## Sleep so it has time to run, or do something else, maybe fill the queue, etc.
while True:
    for i in range(20):
        oc.q.put(i)
    time.sleep(1)