예제 #1
0
 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
     self.target = target
     if name is None:
         self.name = "Tasklet-%d" % (Thread.nTasklet)
         Thread.nTasklet += 1
     else:
         self.name = name
     self.args, self.kwargs = args, kwargs
     self._join = Event()
     self._started = False
     self._alive = False
     self.ident = None
     self._daemon = self._set_daemon()
예제 #2
0
 def __init__(self, group = None, target = None, name = None, args = (), kwargs = {}):
     self.target = target
     if name is None:
         self.name = 'Tasklet-%d' % Thread.nTasklet
         Thread.nTasklet += 1
     else:
         self.name = name
     self.args, self.kwargs = args, kwargs
     self._join = Event()
     set_channel_pref(self._join)
     self._started = False
     self._alive = False
     self.ident = None
     self._daemon = self._set_daemon()
예제 #3
0
class Thread(object):
    nTasklet = 0

    def __init__(self, group = None, target = None, name = None, args = (), kwargs = {}):
        self.target = target
        if name is None:
            self.name = 'Tasklet-%d' % Thread.nTasklet
            Thread.nTasklet += 1
        else:
            self.name = name
        self.args, self.kwargs = args, kwargs
        self._join = Event()
        set_channel_pref(self._join)
        self._started = False
        self._alive = False
        self.ident = None
        self._daemon = self._set_daemon()

    def _set_daemon(self):
        self._daemon = current_thread().daemon

    def __repr__(self):
        status = 'initial'
        if self._started:
            status = 'started'
        if self._daemon:
            status += ' daemon'
        if self.ident is not None:
            status += ' %s' % self.ident
        return '<%s(%s, %s)>' % (self.__class__.__name__, self.name, status)

    def start(self):
        if self._started:
            raise RuntimeError, "Can't start a thread more than once."
        tid = _start_new_thread(self._taskfunc, (self,))
        self.ident = tid
        _active[self.ident] = self
        self._alive = True
        self._started = True

    @staticmethod
    def _taskfunc(self):
        try:
            self.run()
        except Exception:
            traceback.print_exc()
        finally:
            self._alive = False
            del _active[self.ident]
            self._join.set()

    def run(self):
        try:
            if self.target:
                self.target(*self.args, **self.kwargs)
        finally:
            self.target = self.args = self.kwargs = None

    def join(self, timeout = None):
        if not self._started:
            raise RuntimeError, "Can't wait on a not-started thread."
        if currentThread() is self:
            raise RuntimeError, "Can't wait on the same thread."
        self._join.wait(timeout)

    def getName(self):
        return self.name

    def setName(self, name):
        self.name = name

    def get_daemon(self):
        return self._daemon

    def set_daemon(self, val):
        if self._started:
            raise RuntimeError, "Can't change daemon propery after starting"
        self._daemon = val

    daemon = property(get_daemon, set_daemon)

    def isDaemon(self):
        return self.daemon

    def setDaemon(self, daemon):
        self.daemon = daemon

    def is_alive(self):
        return self._alive

    isAlive = is_alive
예제 #4
0
class Thread(object):
    nTasklet = 0
    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        self.target = target
        if name is None:
            self.name = "Tasklet-%d"%(Thread.nTasklet)
            Thread.nTasklet += 1
        else:
            self.name = name
        self.args, self.kwargs = args, kwargs
        self._join = Event()
        set_channel_pref(self._join)
        self._started = False
        self._alive = False
        self.ident = None
        self._daemon = self._set_daemon()
        
    def _set_daemon(self):
        self._daemon = current_thread().daemon        
    
    def __repr__(self):
        status = "initial"
        if self._started:
            status = "started"
        if self._daemon:
            status += " daemon"
        if self.ident is not None:
            status += " %s" % self.ident
        return "<%s(%s, %s)>" % (self.__class__.__name__, self.name, status)    
    
    def start(self):
        if self._started:
            raise RuntimeError, "Can't start a thread more than once."
        tid = _start_new_thread(self._taskfunc, (self,))
        self.ident = tid
        _active[self.ident] = self
        self._alive = True
        self._started = True
 
    @staticmethod
    def _taskfunc(self):
        try:
            self.run()
        except Exception:
            traceback.print_exc()
        finally:
            self._alive = False
            del _active[self.ident]
            self._join.set()
    
    def run(self):
        try:
            if self.target:
                self.target(*self.args, **self.kwargs)
        finally:
            self.target = self.args = self.kwargs = None
        
    def join(self, timeout=None):
        if not self._started:    
            raise RuntimeError, "Can't wait on a not-started thread."
        if currentThread() is self:
            raise RuntimeError, "Can't wait on the same thread."
        self._join.wait(timeout)
        
            
    def getName(self):
        return self.name
    def setName(self, name):
        self.name = name
    
    def get_daemon(self): return self._daemon
    def set_daemon(self, val):
        if self._started:
            raise RuntimeError, "Can't change daemon propery after starting"
        self._daemon = val
    daemon = property(get_daemon, set_daemon)
    
    def isDaemon(self):
        return self.daemon
    def setDaemon(self, daemon):
        self.daemon = daemon
        
    def is_alive(self):
        return self._alive
    isAlive = is_alive