Python threading.current_thread() Examples

The following are code examples for showing how to use threading.current_thread(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: kinect-2-libras   Author: inessadl   File: managers.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 2
Project: kinect-2-libras   Author: inessadl   File: synchronize.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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
# 
Example 3
Project: kinect-2-libras   Author: inessadl   File: _threading_local.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 4
Project: kinect-2-libras   Author: inessadl   File: _threading_local.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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) 
Example 5
Project: wen_spiders   Author: HughWen   File: FlashesSpider.py    (MIT License) View Source Project 6 votes vote down vote up
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') 
Example 6
Project: zmirror   Author: aploium   File: zmirror.py    (MIT License) View Source Project 6 votes vote down vote up
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() 
Example 7
Project: SoCFoundationFlow   Author: mattaw   File: preforkunix.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 8
Project: SoCFoundationFlow   Author: mattaw   File: preforkjava.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 9
Project: SoCFoundationFlow   Author: mattaw   File: prefork.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 10
Project: SoCFoundationFlow   Author: mattaw   File: preforkunix.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 11
Project: SoCFoundationFlow   Author: mattaw   File: preforkjava.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 12
Project: SoCFoundationFlow   Author: mattaw   File: preforkunix.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 13
Project: SoCFoundationFlow   Author: mattaw   File: preforkjava.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 14
Project: SoCFoundationFlow   Author: mattaw   File: prefork.py    (Apache License 2.0) View Source Project 6 votes vote down vote up
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 
Example 15
Project: icrawler   Author: hellock   File: urllist.py    (MIT License) View Source Project 6 votes vote down vote up
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}) 
Example 16
Project: iotronic   Author: openstack   File: agent.py    (license) View Source Project 6 votes vote down vote up
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 
Example 17
Project: iotronic   Author: openstack   File: threadgroup.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 18
Project: routersploit   Author: reverse-shell   File: __init__.py    (license) View Source Project 6 votes vote down vote up
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)) 
Example 19
Project: hostapd-mana   Author: adde88   File: managers.py    (license) View Source Project 6 votes vote down vote up
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 
Example 20
Project: hostapd-mana   Author: adde88   File: synchronize.py    (license) View Source Project 6 votes vote down vote up
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
# 
Example 21
Project: hostapd-mana   Author: adde88   File: test_thread.py    (license) View Source Project 6 votes vote down vote up
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 
Example 22
Project: hostapd-mana   Author: adde88   File: test_thread.py    (license) View Source Project 6 votes vote down vote up
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 
Example 23
Project: hostapd-mana   Author: adde88   File: test_thread.py    (license) View Source Project 6 votes vote down vote up
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 
Example 24
Project: hostapd-mana   Author: adde88   File: _threading_local.py    (license) View Source Project 6 votes vote down vote up
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 
Example 25
Project: hostapd-mana   Author: adde88   File: _threading_local.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 26
Project: python-application   Author: AGProjects   File: threadpool.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 27
Project: llk   Author: Tycx2ry   File: threads.py    (license) View Source Project 6 votes vote down vote up
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. 
Example 28
Project: llk   Author: Tycx2ry   File: control.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 29
Project: llk   Author: Tycx2ry   File: control.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 30
Project: purelove   Author: hucmosin   File: __init__.py    (license) View Source Project 6 votes vote down vote up
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)) 
Example 31
Project: EMFT   Author: 132nd-etcher   File: main_ui_interface.py    (license) View Source Project 6 votes vote down vote up
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 
Example 32
Project: spiderfoot   Author: wi-fi-analyzer   File: control.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 33
Project: spiderfoot   Author: wi-fi-analyzer   File: control.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 34
Project: touch-pay-client   Author: HackPucBemobi   File: dbg.py    (license) View Source Project 6 votes vote down vote up
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 
Example 35
Project: deb-oslo.utils   Author: openstack   File: eventletutils.py    (license) View Source Project 6 votes vote down vote up
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 
Example 36
Project: poseidon   Author: CyberReboot   File: poseidonMonitor.py    (license) View Source Project 6 votes vote down vote up
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() 
Example 37
Project: MMdnn   Author: Microsoft   File: utils.py    (license) View Source Project 6 votes vote down vote up
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 
Example 38
Project: nrp   Author: django-rea   File: lockfile.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 39
Project: pentestly   Author: praetorian-inc   File: threads.py    (license) View Source Project 6 votes vote down vote up
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. 
Example 40
Project: pycos   Author: pgiri   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def scheduler():
        return Pycos._schedulers.get(id(threading.current_thread()), None) 
Example 41
Project: pycos   Author: pgiri   File: __init__.py    (license) View Source Project 5 votes vote down vote up
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 
Example 42
Project: pycos   Author: pgiri   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def scheduler():
        return Pycos._schedulers.get(id(threading.current_thread()), None) 
Example 43
Project: pycos   Author: pgiri   File: __init__.py    (license) View Source Project 5 votes vote down vote up
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 
Example 44
Project: txt2evernote   Author: Xunius   File: out.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def exit(code=0):
        preloader.stop()

        if threading.current_thread().__class__.__name__ == '_MainThread':
            sys.exit(code)
        else:
            thread.exit() 
Example 45
Project: python-   Author: secondtonone1   File: __init__.py    (license) View Source Project 5 votes vote down vote up
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 
Example 46
Project: my-first-blog   Author: AnkurBegining   File: __init__.py    (license) View Source Project 5 votes vote down vote up
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 
Example 47
Project: PyPlanet   Author: PyPlanet   File: management.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 48
Project: PyPlanet   Author: PyPlanet   File: start.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 49
Project: pyelastix   Author: almarklein   File: pyelastix.py    (MIT License) View Source Project 5 votes vote down vote up
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 
Example 50
Project: 4scanner   Author: pboardman   File: downloader.py    (MIT License) View Source Project 5 votes vote down vote up
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