Welcome, guest | Sign In | My Account | Store | Cart

A simple python static class handling basic threading. Designed for "worker" threads that loop forever over a function and the like.

Python, 91 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import threading
import time

class ThM(object):
    """ThM (ThreadManager)
    Handles very simple thread operations:
        Creating single-shot threads -> ThM.run(...)
        Creating 'looping per interval' threads -> ThM.run(...) with loop set to True
        Stopping looping threads based on name -> ThM.stop_loop(...)
        Joining all threads into the calling thread ThM.joinall()
        Removing stopped threads from 'running_threads' - > ThM.free_dead()

     
    The class has been designed for very simple operations, mainly
    for programs that need "workers" that mindlessly loop over a function.

    NOTE: Locks,Events,Semaphores etc. have not been taken into consideration
    and may cause unexpected behaviour if used!
     """
    running_threads = []

    @classmethod
    def run(cls,targetfunc,thname,loop,interval,arglist=[]):
        """Statrs a new thread and appends it to the running_threads list
        along with the specified values.
        Loop and interval needs to be specified even if you dont
        want it to loop over. This is to avoid lot of keyword arguments
        and possible confusion.
        Example of starting a looping thread:
            ThM.run(function,"MyThreadName",True,0.5,[1,2,"StringArguemnt"])

        To stop it, use:
            ThM.stop_loop("MyThreadName")
        Note, a stopped thread cannot be started again!

        Example of a single-shot thread:
            ThM.run(function,"ThreadName",False,0.5,[1,2,"StringArgument"])
            """

        th = threading.Thread(target=cls._thread_runner_,args=(targetfunc,thname,interval,arglist))
        th.setDaemon(True)
        cls.running_threads.append([th,thname,loop])
        th.start()

    @classmethod
    def free_dead(cls):
        """Removes all threads that return FALSE on isAlive() from the running_threads list """
        for th in cls.running_threads[:]:
            if th[0].isAlive() == False:
                cls.running_threads.remove(th)

    @classmethod
    def stop_loop(cls,threadname):
        """Stops a looping function that was started with ThM.run(...)"""
        for i,thlis in enumerate(cls.running_threads):
            if thlis[1] == threadname:
                cls.running_threads[i][2] = False
                break
    
    @classmethod
    def joinall(cls):
        """Joins all the threads together into the calling thread."""
        for th in cls.running_threads[:]:
            while th[0].isAlive():
                time.sleep(0.1)
            th[0].join()
         #   print "Thread:",th[1],"joined","isalive:",th[0].isAlive() --- Debug stuff

    @classmethod
    def get_all_params(cls):
        """Returns parameters from the running_threads list for external manipulation"""
        for thli in cls.running_threads:
            yield(thli[0],thli[1],thli[2])


    #This method is only intended for threads started with ThM !
    @classmethod
    def _thread_runner_(cls,targetfunc,thname,interval,arglist):
        """Internal function handling the running and looping of the threads
        Note: threading.Event() has not been taken into consideration and neither the
        other thread managing objects (semaphores, locks, etc.)"""
        indx=0
        for thread in cls.running_threads[:]:
            if thname == thread[1]:
                break
            indx+=1
        targetfunc(*arglist)
        while cls.running_threads[indx][2] == True:
            targetfunc(*arglist)
            if interval != 0:
                time.sleep(interval)

This class should be used mainly for cases where an infinite loop is required, for example to check for incoming connections, respond based on a Queue or similar. Semaphores, locks and threading.Events have not been considered so they should not be used along with this.

Few important things: Do not call ThM.free_dead() when you have active threads looping over. This will shrink the array and will raise an IndexError in _thread_runner_ Generally the free_dead() function should be called only after joinall(), or if you had previously only single-shot threads and want to clear the list.