python - module - mythread

python  module - mythread


'''
threading.Thread subclass mythread

from mythread import myThread
from mythread import setlogger
from mythread import myThreadParam as myParam
from mythread import T
from mythread import Sparam

def condfunc(mp):
    return not mp.condparam 

def func(mp):
    param = mp.param
    a = param.a0
    b = param.a1
    c = param.a2
    debug(T(mp) + param.name)

def main():
...
    param = Sparam('tname') # struct param
    param.set(a, b, c, d)
    mp = myParam(param, False, condfunc) # new thread param
    
    try:
        t = myThread(tname, func, mp)  #Create and Start a thread
        t.start()
    except:
        error('error in myThread')

    #stop the thread
    mp.condparam = True
    t.stop()


'''

import threading
import time
import sys
#from mylog import logger as log


class Sparam:
    def __init__(self, name):
        self.name = name
        self.a0 = None
        self.a1 = None
        self.a2 = None
        self.a3 = None
        
    def set(self, a0,a1 = None,a2 = None,a3 = None):
        self.a0 = a0
        self.a1 = a1
        self.a2 = a2
        self.a3 = a3


class myThreadParam():
    param = None
    pid = None
    pname = None
    def __init__(self, param, condparam = None, condfunc = None):
        self.param = param # param
        self.condfunc = condfunc # cond func
        self.condparam = condparam # init cond
    def setpid(self, pid):
        self.pid = pid
    def setpname(self, pname):
        self.pname = pname
    def isRunning(self):
        if self.condfunc is None:
            debug('isRunning : condfunc is None')
            return True
        else :
            return self.condfunc(self)
    
logger = None
    
def setlogger(mylog):
    global logger
    logger = mylog

def debug(msg):
    global logger
    if logger is not None:
        logger.debug(msg)

def T(mp):
    if mp is None: return ''
    tag = '(' + str(mp.pid) + ')' + mp.pname + ': '
    return tag

class myThread(threading.Thread):
    def __init__(self, name, func, param):
        debug('Create thread -- ' + name)
        threading.Thread.__init__(self)
        #self.threadID = threadID
        self.name = name
        self.param = param
        self.func = func
        self.__flag = threading.Event()
        self.__flag.set()
        self.__running = threading.Event()
        self.__running.set()
    
    def run(self):
        self.param.setpid(self.ident)
        self.param.setpname(self.name)
        start = time.time()
        debug( "Starting " + self.name + ' at ' + str(start))
        while self.__running.isSet() and self.param.isRunning():
            self.func(self.param)
            #self.__flag.wait(0.5)
        debug( "Exiting " + self.name + ' start at ' + str(start) + ' exit ' + str(time.time()))
    
    def pause(self):
        self.__flag.clear()
        
    def resume(self):
        self.__flag.set()
        
    def stop(self, timeout = None):
        self.__flag.set()
        self.__running.clear()
        debug('mythread: stop ')
        for i in range(10):
            try:
                self.join(timeout)
            except:
                debug(sys.exc_info())
                continue
            break
    
    

	
	

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值