Python threading 模块,current_thread() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用threading.current_thread()

项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def _decref(token, authkey, state, tls, idset, _Client):
        idset.discard(token.id)

        # check whether manager is still alive
        if state is None or state.value == State.STARTED:
            # tell manager this process no longer cares about referent
            try:
                util.debug('DECREF %r', token.id)
                conn = _Client(token.address, authkey=authkey)
                dispatch(conn, None, 'decref', (token.id,))
            except Exception, e:
                util.debug('... decref failed %s', e)

        else:
            util.debug('DECREF %r -- manager already shutdown', token.id)

        # check whether we can close this thread's connection because
        # the process owns no more references to objects for this manager
        if not idset and hasattr(tls, 'connection'):
            util.debug('thread %r has no more proxies so closing conn',
                       threading.current_thread().name)
            tls.connection.close()
            del tls.connection
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def __repr__(self):
        try:
            if self._semlock._is_mine():
                name = current_process().name
                if threading.current_thread().name != 'MainThread':
                    name += '|' + threading.current_thread().name
            elif self._semlock._get_value() == 1:
                name = 'None'
            elif self._semlock._count() > 0:
                name = 'SomeOtherThread'
            else:
                name = 'SomeOtherProcess'
        except Exception:
            name = 'unknown'
        return '<Lock(owner=%s)>' % name

#
# Recursive lock
#
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def __new__(cls, *args, **kw):
        self = object.__new__(cls)
        key = '_local__key', 'thread.local.' + str(id(self))
        object.__setattr__(self, '_local__key', key)
        object.__setattr__(self, '_local__args', (args, kw))
        object.__setattr__(self, '_local__lock', RLock())

        if (args or kw) and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")

        # We need to create the thread dict in anticipation of
        # __init__ being called, to make sure we don't call it
        # again ourselves.
        dict = object.__getattribute__(self, '__dict__')
        current_thread().__dict__[key] = dict

        return self
项目:kinect-2-libras    作者:inessadl    | 项目源码 | 文件源码
def _patch(self):
    key = object.__getattribute__(self, '_local__key')
    d = current_thread().__dict__.get(key)
    if d is None:
        d = {}
        current_thread().__dict__[key] = d
        object.__setattr__(self, '__dict__', d)

        # we have a new instance dict, so call out __init__ if we have
        # one
        cls = type(self)
        if cls.__init__ is not object.__init__:
            args, kw = object.__getattribute__(self, '_local__args')
            cls.__init__(self, *args, **kw)
    else:
        object.__setattr__(self, '__dict__', d)
项目:wen_spiders    作者:HughWen    | 项目源码 | 文件源码
def loop_parse_news_flashes(self, coll):
        while REDIS_CLIENT.scard(self.key) > 0:
            try:
                cur_id = REDIS_CLIENT.spop(self.generate_key())
                per_page = 20
                cur_url = self.news_flashes_tpl % (cur_id.decode("utf-8"), per_page)
                print(threading.current_thread().name + ' is crawling ' + cur_url)
                resp = self.p_get(cur_url).text
                resp_json = json.loads(resp)
                news_list = resp_json['data']['items']
                for news in news_list:
                    news.update({'_id': news['id']})
                    try:
                        if coll.find_one({'_id': news['id']}) is None:
                            self.save_doc(coll, news)
                    except Exception as e:
                        print(e)
            except Exception as e:
                print(e)

        print(threading.current_thread().name + ' is finish')
项目:zmirror    作者:aploium    | 项目源码 | 文件源码
def cron_task_host():
    """??????, ?????????, ???????????"""
    while True:
        # ????????, ??????
        if not enable_cron_tasks:
            if threading.current_thread() != threading.main_thread():
                exit()
            else:
                return

        sleep(60)
        try:
            task_scheduler.run()
        except:  # coverage: exclude
            errprint('ErrorDuringExecutingCronTasks')
            traceback.print_exc()
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:SoCFoundationFlow    作者:mattaw    | 项目源码 | 文件源码
def init_task_pool(self):
        # lazy creation, and set a common pool for all task consumers
        pool = self.pool = []
        for i in range(self.numjobs):
            consumer = Runner.get_pool()
            pool.append(consumer)
            consumer.idx = i
        self.ready = Queue(0)
        def setq(consumer):
            consumer.ready = self.ready
            try:
                threading.current_thread().idx = consumer.idx
            except Exception as e:
                print(e)
        for x in pool:
            x.ready.put(setq)
        return pool
项目:icrawler    作者:hellock    | 项目源码 | 文件源码
def worker_exec(self, queue_timeout=2, **kwargs):
        while True:
            if self.signal.get('reach_max_num'):
                self.logger.info('downloaded image reached max num, thread %s'
                                 ' exit', threading.current_thread().name)
                break
            try:
                url = self.in_queue.get(timeout=queue_timeout)
            except queue.Empty:
                if self.signal.get('feeder_exited'):
                    self.logger.info('no more page urls to parse, thread %s'
                                     ' exit', threading.current_thread().name)
                    break
                else:
                    self.logger.info('%s is waiting for new page urls',
                                     threading.current_thread().name)
                    continue
            except Exception as e:
                self.logger.error('exception caught in thread %s: %s',
                                  threading.current_thread().name, e)
                continue
            else:
                self.logger.debug('start downloading page {}'.format(url))
            self.output({'file_url': url})
项目:iotronic    作者:openstack    | 项目源码 | 文件源码
def wamp_request(e, kwarg, session):
    id = threading.current_thread().ident
    shared_result[id] = {}
    shared_result[id]['result'] = None

    def success(d):
        shared_result[id]['result'] = d
        LOG.debug("DEVICE sent: %s", str(d))
        e.set()
        return shared_result[id]['result']

    def fail(failure):
        shared_result[id]['result'] = failure
        LOG.error("WAMP FAILURE: %s", str(failure))
        e.set()
        return shared_result[id]['result']

    LOG.debug("Calling %s...", kwarg['wamp_rpc_call'])
    d = session.wamp_session.call(wamp_session_caller,
                                  kwarg['wamp_rpc_call'], *kwarg['data'])
    d.addCallback(success)
    d.addErrback(fail)


# OSLO ENDPOINT
项目:iotronic    作者:openstack    | 项目源码 | 文件源码
def wait(self):
        for x in self.timers:
            try:
                x.wait()
            except eventlet.greenlet.GreenletExit:
                pass
            except Exception as ex:
                LOG.exception(ex)
        current = threading.current_thread()

        # Iterate over a copy of self.threads so thread_done doesn't
        # modify the list while we're iterating
        for x in self.threads[:]:
            if x is current:
                continue
            try:
                x.wait()
            except eventlet.greenlet.GreenletExit:
                pass
            except Exception as ex:
                LOG.exception(ex)
项目:routersploit    作者:reverse-shell    | 项目源码 | 文件源码
def __cprint(*args, **kwargs):
    """ Color print()

    Signature like Python 3 print() function
    print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
    """
    if not kwargs.pop("verbose", True):
        return

    color = kwargs.get('color', None)
    sep = kwargs.get('sep', ' ')
    end = kwargs.get('end', '\n')
    thread = threading.current_thread()
    try:
        file_ = thread_output_stream.get(thread, ())[-1]
    except IndexError:
        file_ = kwargs.get('file', sys.stdout)

    if color:
        printer_queue.put(PrintResource(content='\033[{}m'.format(colors[color]), end='', file=file_, sep=sep, thread=thread))
        printer_queue.put(PrintResource(content=args, end='', file=file_, sep=sep, thread=thread))  # TODO printing text that starts from newline
        printer_queue.put(PrintResource(content='\033[0m', sep=sep, end=end, file=file_, thread=thread))
    else:
        printer_queue.put(PrintResource(content=args, sep=sep, end=end, file=file_, thread=thread))
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _decref(token, authkey, state, tls, idset, _Client):
        idset.discard(token.id)

        # check whether manager is still alive
        if state is None or state.value == State.STARTED:
            # tell manager this process no longer cares about referent
            try:
                util.debug('DECREF %r', token.id)
                conn = _Client(token.address, authkey=authkey)
                dispatch(conn, None, 'decref', (token.id,))
            except Exception, e:
                util.debug('... decref failed %s', e)

        else:
            util.debug('DECREF %r -- manager already shutdown', token.id)

        # check whether we can close this thread's connection because
        # the process owns no more references to objects for this manager
        if not idset and hasattr(tls, 'connection'):
            util.debug('thread %r has no more proxies so closing conn',
                       threading.current_thread().name)
            tls.connection.close()
            del tls.connection
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def __repr__(self):
        try:
            if self._semlock._is_mine():
                name = current_process().name
                if threading.current_thread().name != 'MainThread':
                    name += '|' + threading.current_thread().name
                count = self._semlock._count()
            elif self._semlock._get_value() == 1:
                name, count = 'None', 0
            elif self._semlock._count() > 0:
                name, count = 'SomeOtherThread', 'nonzero'
            else:
                name, count = 'SomeOtherProcess', 'nonzero'
        except Exception:
            name, count = 'unknown', 'unknown'
        return '<RLock(%s, %s)>' % (name, count)

#
# Condition variable
#
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def readerThread(self, d, readerNum):
        if sys.version_info[0] < 3 :
            name = currentThread().getName()
        else :
            name = currentThread().name

        for i in xrange(5) :
            c = d.cursor()
            count = 0
            rec = c.first()
            while rec:
                count += 1
                key, data = rec
                self.assertEqual(self.makeData(key), data)
                rec = c.next()
            if verbose:
                print "%s: found %d records" % (name, count)
            c.close()

        if verbose:
            print "%s: thread finished" % name
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def writerThread(self, d, keys, readers):
        if sys.version_info[0] < 3 :
            name = currentThread().getName()
        else :
            name = currentThread().name
        if verbose:
            print "%s: creating records %d - %d" % (name, start, stop)

        count=len(keys)//len(readers)
        count2=count
        for x in keys :
            key = '%04d' % x
            dbutils.DeadlockWrap(d.put, key, self.makeData(key),
                                 max_retries=12)

            if verbose and x % 100 == 0:
                print "%s: records %d - %d finished" % (name, start, x)

            count2-=1
            if not count2 :
                readers.pop().start()
                count2=count

        if verbose:
            print "%s: thread finished" % name
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def readerThread(self, d, readerNum):
        if sys.version_info[0] < 3 :
            name = currentThread().getName()
        else :
            name = currentThread().name

        c = d.cursor()
        count = 0
        rec = dbutils.DeadlockWrap(c.first, max_retries=10)
        while rec:
            count += 1
            key, data = rec
            self.assertEqual(self.makeData(key), data)
            rec = dbutils.DeadlockWrap(c.next, max_retries=10)
        if verbose:
            print "%s: found %d records" % (name, count)
        c.close()

        if verbose:
            print "%s: thread finished" % name
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def __new__(cls, *args, **kw):
        self = object.__new__(cls)
        key = '_local__key', 'thread.local.' + str(id(self))
        object.__setattr__(self, '_local__key', key)
        object.__setattr__(self, '_local__args', (args, kw))
        object.__setattr__(self, '_local__lock', RLock())

        if (args or kw) and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")

        # We need to create the thread dict in anticipation of
        # __init__ being called, to make sure we don't call it
        # again ourselves.
        dict = object.__getattribute__(self, '__dict__')
        current_thread().__dict__[key] = dict

        return self
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _patch(self):
    key = object.__getattribute__(self, '_local__key')
    d = current_thread().__dict__.get(key)
    if d is None:
        d = {}
        current_thread().__dict__[key] = d
        object.__setattr__(self, '__dict__', d)

        # we have a new instance dict, so call out __init__ if we have
        # one
        cls = type(self)
        if cls.__init__ is not object.__init__:
            args, kw = object.__getattribute__(self, '_local__args')
            cls.__init__(self, *args, **kw)
    else:
        object.__setattr__(self, '__dict__', d)
项目:python-application    作者:AGProjects    | 项目源码 | 文件源码
def _worker(self):
        thread = current_thread()
        while True:
            task = self._queue.get()
            if task is self.StopWorker:
                break
            # noinspection PyBroadException
            try:
                task.function(*task.args, **task.kw)
            except:
                log.exception('Unhandled exception while calling %r in the %r thread' % (task.function, thread.name))
            finally:
                with self._lock:
                    self.__dict__['jobs'] -= 1
                del task
        self._threads.remove(thread)
项目:llk    作者:Tycx2ry    | 项目源码 | 文件源码
def _thread_wrapper(self, *args):
        ''' Wrapper for the worker method defined in the module. Handles calling the actual worker, cleanly exiting upon
        interrupt, and passing exceptions back to the main process.'''
        thread_name = threading.current_thread().name
        self.debug('THREAD => %s started.' % thread_name)
        while not self.stopped.is_set():
            try:
                # use the get_nowait() method for retrieving a queued item to
                # prevent the thread from blocking when the queue is empty
                obj = self.q.get_nowait()
            except Empty:
                continue
            try:
                # launch the public module_thread method
                self.module_thread(obj, *args)
            except:
                # handle exceptions local to the thread
                self.print_exception('(thread=%s, object=%s)' % (thread_name, repr(obj)))
            finally:
                self.q.task_done()
        self.debug('THREAD => %s exited.' % thread_name)

    # sometimes a keyboardinterrupt causes a race condition between when the self.q.task_done() call above and the
    # self.q.empty() call below, causing all the threads to hang. introducing the time.sleep(.7) call below reduces
    # the likelihood of encountering the race condition.
项目:llk    作者:Tycx2ry    | 项目源码 | 文件源码
def close(self):
    """
    Closes our socket connection. This is a pass-through for our socket's
    :func:`~stem.socket.ControlSocket.close` method.
    """

    self._socket.close()

    # Join on any outstanding state change listeners. Closing is a state change
    # of its own, so if we have any listeners it's quite likely there's some
    # work in progress.
    #
    # It's important that we do this outside of our locks so those daemons have
    # access to us. This is why we're doing this here rather than _close().

    for t in self._state_change_threads:
      if t.is_alive() and threading.current_thread() != t:
        t.join()
项目:llk    作者:Tycx2ry    | 项目源码 | 文件源码
def _close(self):
    # Our is_alive() state is now false. Our reader thread should already be
    # awake from recv() raising a closure exception. Wake up the event thread
    # too so it can end.

    self._event_notice.set()
    self._is_authenticated = False

    # joins on our threads if it's safe to do so

    for t in (self._reader_thread, self._event_thread):
      if t and t.is_alive() and threading.current_thread() != t:
        t.join()

    self._notify_status_listeners(State.CLOSED)

    self._socket_close()
项目:purelove    作者:hucmosin    | 项目源码 | 文件源码
def __cprint(*args, **kwargs):
    """ Color print()

    Signature like Python 3 print() function
    print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
    """
    if not kwargs.pop("verbose", True):
        return

    color = kwargs.get('color', None)
    sep = kwargs.get('sep', ' ')
    end = kwargs.get('end', '\n')
    thread = threading.current_thread()
    try:
        file_ = thread_output_stream.get(thread, ())[-1]
    except IndexError:
        file_ = kwargs.get('file', sys.stdout)

    if color:
        printer_queue.put(PrintResource(content='\033[{}m'.format(colors[color]), end='', file=file_, sep=sep, thread=thread))
        printer_queue.put(PrintResource(content=args, end='', file=file_, sep=sep, thread=thread))  # TODO printing text that starts from newline
        printer_queue.put(PrintResource(content='\033[0m', sep=sep, end=end, file=file_, thread=thread))
    else:
        printer_queue.put(PrintResource(content=args, sep=sep, end=end, file=file_, thread=thread))
项目:EMFT    作者:132nd-etcher    | 项目源码 | 文件源码
def __call__(self, *args, **kwargs):
        if constant.MAIN_UI is None:
            raise RuntimeError('Main UI not initialized')

        # Check if this was called in the main thread (the one running Qt EventLoop)
        # noinspection PyProtectedMember
        if isinstance(threading.current_thread(), threading._MainThread):

            # If we are in the EventLoop, there's no need to dispatch the call to the a signal
            # noinspection PyProtectedMember
            constant.MAIN_UI._do('main_ui', self.func.__name__, args, kwargs)
        else:

            # Otherwise, queue the call
            constant.MAIN_UI.do('main_ui', self.func.__name__, *args, **kwargs)


# noinspection PyAbstractClass
项目:spiderfoot    作者:wi-fi-analyzer    | 项目源码 | 文件源码
def close(self):
    """
    Closes our socket connection. This is a pass-through for our socket's
    :func:`~stem.socket.ControlSocket.close` method.
    """

    self._socket.close()

    # Join on any outstanding state change listeners. Closing is a state change
    # of its own, so if we have any listeners it's quite likely there's some
    # work in progress.
    #
    # It's important that we do this outside of our locks so those daemons have
    # access to us. This is why we're doing this here rather than _close().

    for t in self._state_change_threads:
      if t.is_alive() and threading.current_thread() != t:
        t.join()
项目:spiderfoot    作者:wi-fi-analyzer    | 项目源码 | 文件源码
def _close(self):
    # Our is_alive() state is now false. Our reader thread should already be
    # awake from recv() raising a closure exception. Wake up the event thread
    # too so it can end.

    self._event_notice.set()
    self._is_authenticated = False

    # joins on our threads if it's safe to do so

    for t in (self._reader_thread, self._event_thread):
      if t and t.is_alive() and threading.current_thread() != t:
        t.join()

    self._notify_status_listeners(State.CLOSED)

    self._socket_close()
项目:touch-pay-client    作者:HackPucBemobi    | 项目源码 | 文件源码
def startup(self):
        "Notify and wait frontend to set initial params and breakpoints"
        # send some useful info to identify session
        thread = threading.current_thread()
        # get the caller module filename
        frame = sys._getframe()
        fn = self.canonic(frame.f_code.co_filename)
        while frame.f_back and self.canonic(frame.f_code.co_filename) == fn:
            frame = frame.f_back
        args = [__version__, os.getpid(), thread.name, " ".join(sys.argv),
                frame.f_code.co_filename]
        self.pipe.send({'method': 'startup', 'args': args})
        while self.pull_actions() is not None:
            pass

    # General interaction function
项目:deb-oslo.utils    作者:openstack    | 项目源码 | 文件源码
def fetch_current_thread_functor():
    """Get the current thread.

    If eventlet is used to monkey-patch the threading module, return the
    current eventlet greenthread. Otherwise, return the current Python thread.

    .. versionadded:: 1.5
    """
    # Until https://github.com/eventlet/eventlet/issues/172 is resolved
    # or addressed we have to use complicated workaround to get a object
    # that will not be recycled; the usage of threading.current_thread()
    # doesn't appear to currently be monkey patched and therefore isn't
    # reliable to use (and breaks badly when used as all threads share
    # the same current_thread() object)...
    if not EVENTLET_AVAILABLE:
        return threading.current_thread
    else:
        green_threaded = _patcher.is_monkey_patched('thread')
        if green_threaded:
            return _eventlet.getcurrent
        else:
            return threading.current_thread
项目:poseidon    作者:CyberReboot    | 项目源码 | 文件源码
def schedule_thread_worker(schedule, logger):
    ''' schedule thread, takes care of running processes in the future '''
    global CTRL_C
    logLine = 'starting thread_worker'
    logger.debug(logLine)
    while not CTRL_C['STOP']:
        #print('looping', CTRL_C)
        sys.stdout.flush()
        schedule.run_pending()
        logLine = 'scheduler woke {0}'.format(
            threading.current_thread().getName())
        time.sleep(1)
        logger.debug(logLine)
    logger.debug('Threading stop:{0}'.format(
        threading.current_thread().getName()))
    sys.exit()
项目:MMdnn    作者:Microsoft    | 项目源码 | 文件源码
def _multi_thread_download(url, file_name, file_size, thread_count):
    import threading
    fp = open(file_name, "wb")
    fp.truncate(file_size)
    fp.close()

    part = file_size // thread_count
    for i in range(thread_count):
        start = part * i
        if i == thread_count - 1:
            end = file_size
        else:
            end = start + part

        t = threading.Thread(target=_downloader, kwargs={'start': start, 'end': end, 'url': url, 'filename': file_name})
        t.setDaemon(True)
        t.start()

    main_thread = threading.current_thread()
    for t in threading.enumerate():
        if t is main_thread:
            continue
        t.join()

    return file_name
项目:nrp    作者:django-rea    | 项目源码 | 文件源码
def __init__(self, path, threaded=True):
        """
        >>> lock = LockBase("somefile")
        >>> lock = LockBase("somefile", threaded=False)
        """
        self.path = path
        self.lock_file = os.path.abspath(path) + ".lock"
        self.hostname = socket.gethostname()
        self.pid = os.getpid()
        app_name = "-test"
        if threaded:
            name = threading.current_thread().get_name()
            tname = "%s-" % quote(name, safe="")
        else:
            tname = ""
        dirname = os.path.dirname(self.lock_file)
        self.unique_name = os.path.join(dirname,
                                        "%s%s.%s%s" % (self.hostname,
                                                     app_name,
                                                     tname,
                                                     self.pid))
        msg = " ".join(["lock_file:", self.lock_file])
        logger.debug(msg)
        msg = " ".join(["unique_name:", self.unique_name])
        logger.debug(msg)
项目:pentestly    作者:praetorian-inc    | 项目源码 | 文件源码
def _thread_wrapper(self, *args):
        ''' Wrapper for the worker method defined in the module. Handles calling the actual worker, cleanly exiting upon
        interrupt, and passing exceptions back to the main process.'''
        thread_name = threading.current_thread().name
        self.debug('THREAD => %s started.' % thread_name)
        while not self.stopped.is_set():
            try:
                # use the get_nowait() method for retrieving a queued item to
                # prevent the thread from blocking when the queue is empty
                obj = self.q.get_nowait()
            except Empty:
                continue
            try:
                # launch the public module_thread method
                self.module_thread(obj, *args)
            except:
                # handle exceptions local to the thread
                self.print_exception('(thread=%s, object=%s)' % (thread_name, repr(obj)))
            finally:
                self.q.task_done()
        self.debug('THREAD => %s exited.' % thread_name)

    # sometimes a keyboardinterrupt causes a race condition between when the self.q.task_done() call above and the
    # self.q.empty() call below, causing all the threads to hang. introducing the time.sleep(.7) call below reduces
    # the likelihood of encountering the race condition.
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def scheduler():
        return Pycos._schedulers.get(id(threading.current_thread()), None)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def cur_task(scheduler=None):
        """Must be called from a task only.
        """
        if not scheduler:
            scheduler = Pycos._schedulers.get(id(threading.current_thread()), None)
            if not scheduler:
                return None
        return scheduler.__cur_task
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def scheduler():
        return Pycos._schedulers.get(id(threading.current_thread()), None)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def cur_task(scheduler=None):
        """Must be called from a task only.
        """
        if not scheduler:
            scheduler = Pycos._schedulers.get(id(threading.current_thread()), None)
            if not scheduler:
                return None
        return scheduler.__cur_task
项目:txt2evernote    作者:Xunius    | 项目源码 | 文件源码
def exit(code=0):
        preloader.stop()

        if threading.current_thread().__class__.__name__ == '_MainThread':
            sys.exit(code)
        else:
            thread.exit()
项目:python-    作者:secondtonone1    | 项目源码 | 文件源码
def __init__(self, path, threaded=True, timeout=None):
        """
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        """
        super(LockBase, self).__init__(path)
        self.lock_file = os.path.abspath(path) + ".lock"
        self.hostname = socket.gethostname()
        self.pid = os.getpid()
        if threaded:
            t = threading.current_thread()
            # Thread objects in Python 2.4 and earlier do not have ident
            # attrs.  Worm around that.
            ident = getattr(t, "ident", hash(t))
            self.tname = "-%x" % (ident & 0xffffffff)
        else:
            self.tname = ""
        dirname = os.path.dirname(self.lock_file)

        # unique name is mostly about the current process, but must
        # also contain the path -- otherwise, two adjacent locked
        # files conflict (one file gets locked, creating lock-file and
        # unique file, the other one gets locked, creating lock-file
        # and overwriting the already existing lock-file, then one
        # gets unlocked, deleting both lock-file and unique file,
        # finally the last lock errors out upon releasing.
        self.unique_name = os.path.join(dirname,
                                        "%s%s.%s%s" % (self.hostname,
                                                       self.tname,
                                                       self.pid,
                                                       hash(self.path)))
        self.timeout = timeout
项目:my-first-blog    作者:AnkurBegining    | 项目源码 | 文件源码
def __init__(self, path, threaded=True, timeout=None):
        """
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        """
        super(LockBase, self).__init__(path)
        self.lock_file = os.path.abspath(path) + ".lock"
        self.hostname = socket.gethostname()
        self.pid = os.getpid()
        if threaded:
            t = threading.current_thread()
            # Thread objects in Python 2.4 and earlier do not have ident
            # attrs.  Worm around that.
            ident = getattr(t, "ident", hash(t))
            self.tname = "-%x" % (ident & 0xffffffff)
        else:
            self.tname = ""
        dirname = os.path.dirname(self.lock_file)

        # unique name is mostly about the current process, but must
        # also contain the path -- otherwise, two adjacent locked
        # files conflict (one file gets locked, creating lock-file and
        # unique file, the other one gets locked, creating lock-file
        # and overwriting the already existing lock-file, then one
        # gets unlocked, deleting both lock-file and unique file,
        # finally the last lock errors out upon releasing.
        self.unique_name = os.path.join(dirname,
                                        "%s%s.%s%s" % (self.hostname,
                                                       self.tname,
                                                       self.pid,
                                                       hash(self.path)))
        self.timeout = timeout
项目:PyPlanet    作者:PyPlanet    | 项目源码 | 文件源码
def execute(self):
        # Parse arguments.
        self.arguments = self.parser.parse_args()

        # Initiate the logger.
        threading.current_thread().setName('Main')
        initiate_logger()
        self.logger = logging.getLogger(__name__)

        # Initiate the settings by accessing one.
        self.logger.debug('Initiated configuration and environment... (debug on, means no error reporting and verbose output')
        if not settings.DEBUG:
            self.logger.info('Initiated configuration and environment...')
        self.logger.info('-------------------------------[  PyPlanet v{}  ]-------------------------------'.format(version))

        # Start god process (the current started process).
        pool = EnvironmentPool(settings.POOLS, max_restarts=self.arguments.max_restarts)
        pool.populate()

        # Starting all processes.
        pool.start()

        # Start the watchdog.
        try:
            pool.watchdog()
        except KeyboardInterrupt:
            pool.shutdown()
            exit(0)
项目:PyPlanet    作者:PyPlanet    | 项目源码 | 文件源码
def handle(self, *args, **options):
        # Detach when asked.
        if 'detach' in options and options['detach']:
            self.detach(pid_file=options['pid_file'] if 'pid_file' in options and options['pid_file'] else 'pyplanet.pid')

        # Initiate the logger.
        threading.current_thread().setName('Main')
        initiate_logger()
        logger = logging.getLogger(__name__)

        # Initiate the settings by accessing one.
        logger.debug('Initiated configuration and environment... (debug on, means no error reporting and verbose output')
        if not settings.DEBUG:
            logger.info('Initiated configuration and environment...')
        logger.info('-------------------------------[  PyPlanet v{}  ]-------------------------------'.format(self.get_version()))

        # Start god process (the current started process).
        self.pool = EnvironmentPool(settings.POOLS, max_restarts=options['max_restarts'], options=options)
        self.pool.populate()

        # Starting all processes.
        self.pool.start()

        # Start the watchdog.
        try:
            self.pool.watchdog()
        except KeyboardInterrupt:
            self.pool.shutdown()
            exit(0)
项目:pyelastix    作者:almarklein    | 项目源码 | 文件源码
def get_tempdir():
    """ Get the temporary directory where pyelastix stores its temporary
    files. The directory is specific to the current process and the
    calling thread. Generally, the user does not need this; directories
    are automatically cleaned up. Though Elastix log files are also
    written here.
    """
    tempdir = os.path.join(tempfile.gettempdir(), 'pyelastix')

    # Make sure it exists
    if not os.path.isdir(tempdir):
        os.makedirs(tempdir)

    # Clean up all directories for which the process no longer exists
    for fname in os.listdir(tempdir):
        dirName = os.path.join(tempdir, fname)
        # Check if is right kind of dir
        if not (os.path.isdir(dirName) and  fname.startswith('id_')):
            continue
        # Get pid and check if its running
        try:
            pid = int(fname.split('_')[1])
        except Exception:
            continue
        if not _is_pid_running(pid):
            _clear_dir(dirName)

    # Select dir that included process and thread id
    tid = id(threading.current_thread() if hasattr(threading, 'current_thread')
                                        else threading.currentThread())
    dir = os.path.join(tempdir, 'id_%i_%i' % (os.getpid(), tid))
    if not os.path.isdir(dir):
        os.mkdir(dir)
    return dir
项目:4scanner    作者:pboardman    | 项目源码 | 文件源码
def __init__(self, thread_nb, board, imageboard, output_folder, folder, is_quiet, condition, check_duplicate):
        # Getting info about the imageboard URL
        ib_info = imageboard_info.imageboard_info(imageboard)

        base_url = ib_info.base_url
        image_url = ib_info.image_base_url
        thread_subfolder = ib_info.thread_subfolder
        image_subfolder = ib_info.image_subfolder

        # These URL are the url of the thread
        # and the base url where images are stored on the imageboard
        self.thread_url = "{0}{1}{2}{3}.json".format(base_url, board, thread_subfolder, thread_nb)
        self.image_url = "{0}{1}{2}".format(image_url, board, image_subfolder)

        self.tmp_dir = "/tmp/{0}/".format(os.getpid())
        self.curr_time = time.strftime('%d%m%Y-%H%M%S')
        self.pid = os.getpid()
        self.thread = threading.current_thread().name

        self.downloaded_log = "{0}/{1}4scanner_dld-{2}-{3}".format(self.tmp_dir, self.curr_time, self.pid, self.thread)

        self.out_dir = os.path.join(output_folder, 'downloads', imageboard, board, folder, str(thread_nb))

        self.thread_nb = thread_nb
        self.imageboard = imageboard
        self.board = board
        self.condition = condition
        self.check_duplicate = check_duplicate
        self.is_quiet = is_quiet

        # Creating the tmp and output directory
        self.create_dir(self.tmp_dir)
        self.create_dir(self.out_dir)


    # Main download function