Python threading.main_thread() Examples

The following are code examples for showing how to use threading.main_thread(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: zmirror   Author: aploium   File: zmirror.py    MIT License 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 2
Project: vscode-mayapy   Author: FXTD-ODYSSEY   File: test_utilities.py    MIT License 6 votes vote down vote up
def test_is_main_thread():
    from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread
    if not is_current_thread_main_thread():
        error_msg = 'Current thread does not seem to be a main thread. Details:\n'
        current_thread = threading.current_thread()
        error_msg += 'Current thread: %s\n' % (current_thread,)

        if hasattr(threading, 'main_thread'):
            error_msg += 'Main thread found: %s\n' % (threading.main_thread(),)
        else:
            error_msg += 'Current main thread not instance of: %s (%s)' % (
                threading._MainThread, current_thread.__class__.__mro__,)

        raise AssertionError(error_msg)

    class NonMainThread(threading.Thread):

        def run(self):
            self.is_main_thread = is_current_thread_main_thread()

    non_main_thread = NonMainThread()
    non_main_thread.start()
    non_main_thread.join()
    assert not non_main_thread.is_main_thread 
Example 3
Project: NiujiaoDebugger   Author: MrSrc   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork(self):
        code = """if 1:
            import os, threading

            pid = os.fork()
            if pid == 0:
                main = threading.main_thread()
                print(main.name)
                print(main.ident == threading.current_thread().ident)
                print(main.ident == threading.get_ident())
            else:
                os.waitpid(pid, 0)
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "MainThread\nTrue\nTrue\n") 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork_from_nonmain_thread(self):
        code = """if 1:
            import os, threading, sys

            def f():
                pid = os.fork()
                if pid == 0:
                    main = threading.main_thread()
                    print(main.name)
                    print(main.ident == threading.current_thread().ident)
                    print(main.ident == threading.get_ident())
                    # stdout is fully buffered because not a tty,
                    # we have to flush before exit.
                    sys.stdout.flush()
                else:
                    os.waitpid(pid, 0)

            th = threading.Thread(target=f)
            th.start()
            th.join()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "Thread-1\nTrue\nTrue\n") 
Example 5
Project: NiujiaoDebugger   Author: MrSrc   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_3_join_in_forked_from_thread(self):
        # Like the test above, but fork() was called from a worker thread
        # In the forked process, the main Thread object must be marked as stopped.

        script = """if 1:
            main_thread = threading.current_thread()
            def worker():
                childpid = os.fork()
                if childpid != 0:
                    os.waitpid(childpid, 0)
                    sys.exit(0)

                t = threading.Thread(target=joiningfunc,
                                     args=(main_thread,))
                print('end of main')
                t.start()
                t.join() # Should not block: main_thread is already stopped

            w = threading.Thread(target=worker)
            w.start()
            """
        self._run_and_join(script) 
Example 6
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork(self):
        code = """if 1:
            import os, threading

            pid = os.fork()
            if pid == 0:
                main = threading.main_thread()
                print(main.name)
                print(main.ident == threading.current_thread().ident)
                print(main.ident == threading.get_ident())
            else:
                os.waitpid(pid, 0)
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "MainThread\nTrue\nTrue\n") 
Example 7
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork_from_nonmain_thread(self):
        code = """if 1:
            import os, threading, sys

            def f():
                pid = os.fork()
                if pid == 0:
                    main = threading.main_thread()
                    print(main.name)
                    print(main.ident == threading.current_thread().ident)
                    print(main.ident == threading.get_ident())
                    # stdout is fully buffered because not a tty,
                    # we have to flush before exit.
                    sys.stdout.flush()
                else:
                    os.waitpid(pid, 0)

            th = threading.Thread(target=f)
            th.start()
            th.join()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "Thread-1\nTrue\nTrue\n") 
Example 8
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_3_join_in_forked_from_thread(self):
        # Like the test above, but fork() was called from a worker thread
        # In the forked process, the main Thread object must be marked as stopped.

        script = """if 1:
            main_thread = threading.current_thread()
            def worker():
                childpid = os.fork()
                if childpid != 0:
                    os.waitpid(childpid, 0)
                    sys.exit(0)

                t = threading.Thread(target=joiningfunc,
                                     args=(main_thread,))
                print('end of main')
                t.start()
                t.join() # Should not block: main_thread is already stopped

            w = threading.Thread(target=worker)
            w.start()
            """
        self._run_and_join(script) 
Example 9
Project: envoxy   Author: habitio   File: watchdog.py    MIT License 6 votes vote down vote up
def send_notification(self):
        try:
            from systemd.daemon import notify
            event = threading.Event()

            while not event.wait(self.interval - 1):
                main_thread_alive = threading.main_thread().is_alive()
                if main_thread_alive:

                    if 'last_event_ms' in uwsgi.opt and time.time() - uwsgi.opt['last_event_ms'] <= self.interval or self._test_http():
                        log.verbose('[{}] Watchdog sent successfully'.format(log.style.apply('OK', log.style.GREEN_FG)))
                        notify('WATCHDOG=1')

        except (KeyError, TypeError, ValueError) as e:
            log.error('[{}] Error {}'.format(log.style.apply('Watchdog', log.style.RED_FG), e))
        except ImportError:
            log.warning('[{}] systemd not imported {}'.format(log.style.apply('Watchdog', log.style.RED_FG), traceback.format_exc(limit=5)))
        except:
            log.alert('[{}] Unexpected exception {}'.format(log.style.apply('Watchdog', log.style.RED_FG), traceback.format_exc(limit=5))) 
Example 10
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threading.py    GNU General Public License v2.0 6 votes vote down vote up
def test_main_thread_after_fork(self):
        code = """if 1:
            import os, threading

            pid = os.fork()
            if pid == 0:
                main = threading.main_thread()
                print(main.name)
                print(main.ident == threading.current_thread().ident)
                print(main.ident == threading.get_ident())
            else:
                os.waitpid(pid, 0)
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "MainThread\nTrue\nTrue\n") 
Example 11
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threading.py    GNU General Public License v2.0 6 votes vote down vote up
def test_main_thread_after_fork_from_nonmain_thread(self):
        code = """if 1:
            import os, threading, sys

            def f():
                pid = os.fork()
                if pid == 0:
                    main = threading.main_thread()
                    print(main.name)
                    print(main.ident == threading.current_thread().ident)
                    print(main.ident == threading.get_ident())
                    # stdout is fully buffered because not a tty,
                    # we have to flush before exit.
                    sys.stdout.flush()
                else:
                    os.waitpid(pid, 0)

            th = threading.Thread(target=f)
            th.start()
            th.join()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "Thread-1\nTrue\nTrue\n") 
Example 12
Project: r2-d7   Author: FreakyDug   File: test_core.py    MIT License 6 votes vote down vote up
def test_threaded(testbot):
    def threadtest(signal):
        # If a new event loop isn't created for the thread, this will crash
        try:
            assert threading.current_thread() != threading.main_thread()
            testbot.load_data()
        except Exception as error:
            # Pytest will catch this stdout and print it and the signal will
            # fail the test
            print(error)
            signal.clear()
        else:
            signal.set()

    signal = threading.Event()
    thread = threading.Thread(target=threadtest, args=(signal, ))
    thread.start()
    thread.join()
    assert signal.is_set() 
Example 13
Project: zmirror   Author: ttestdock   File: zmirror.py    MIT License 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 14
Project: baboossh   Author: cybiere   File: params.py    GNU General Public License v3.0 6 votes vote down vote up
def connect(cls,workspace):
        dbPath = join(workspacesDir,workspace,"workspace.db")
        mainThreadName = threading.main_thread().getName()
        currentName = threading.currentThread().getName()
        if currentName != mainThreadName:
            if currentName in cls.__threadsConn.keys():
                return
            else:
                cls.__threadsConn[currentName] = sqlite3.connect(dbPath)
                return
        if cls.__conn is not None:
            cls.__conn.close()
        cls.__workspace = workspace
        if not exists(dbPath):
            raise ValueError("Workspace database not found, the workspace must be corrupted !")
        cls.__conn = sqlite3.connect(dbPath) 
Example 15
Project: baboossh   Author: cybiere   File: workspace.py    GNU General Public License v3.0 6 votes vote down vote up
def massConnect(self,verbose):
        try:
            endpoints,users,creds = self.parseOptionsTarget()
        except:
            return
        nbIter = len(endpoints)*len(users)*len(creds)
        if nbIter == 1:
            self.connect(endpoints[0],users[0],creds[0],verbose)
            return

        if not yesNo("This will attempt up to "+str(nbIter)+" connections. Proceed ?",False):
            return
        
        for endpoint in endpoints:
            t = threading.Thread(target=self.threadConnect, args=(verbose,endpoint,users,creds))
            t.start()
        main_thread = threading.main_thread()
        for t in threading.enumerate():
            if t is main_thread:
                continue
            t.join() 
Example 16
Project: cobald   Author: MatterMiners   File: asyncio_watcher.py    MIT License 6 votes vote down vote up
def asyncio_main_run(root_runner: BaseRunner):
    """
    Create an ``asyncio`` event loop running in the main thread and watching runners

    Using ``asyncio`` to handle subprocesses requires a specific loop type
    to run in the main thread.
    This function sets up and runs the correct loop in a portable way.
    In addition, it runs a single :py:class:`~.BaseRunner` until completion
    or failure.

    .. seealso:: The `issue #8 <https://github.com/MatterMiners/cobald/issues/8>`_
                 for details.
    """
    assert (
        threading.current_thread() == threading.main_thread()
    ), "only main thread can accept asyncio subprocesses"
    if sys.platform == "win32":
        event_loop = asyncio.ProactorEventLoop()
        asyncio.set_event_loop(event_loop)
    else:
        event_loop = asyncio.get_event_loop()
        asyncio.get_child_watcher().attach_loop(event_loop)
    event_loop.run_until_complete(awaitable_runner(root_runner)) 
Example 17
Project: cobald   Author: MatterMiners   File: meta_runner.py    MIT License 6 votes vote down vote up
def run(self):
        """Run all runners, blocking until completion or error"""
        self._logger.info("starting all runners")
        try:
            with self._lock:
                assert not self.running.set(), "cannot re-run: %s" % self
                self.running.set()
            thread_runner = self.runners[threading]
            for runner in self.runners.values():
                if runner is not thread_runner:
                    thread_runner.register_payload(runner.run)
            if threading.current_thread() == threading.main_thread():
                asyncio_main_run(root_runner=thread_runner)
            else:
                thread_runner.run()
        except Exception as err:
            self._logger.exception("runner terminated: %s", err)
            raise RuntimeError from err
        finally:
            self._stop_runners()
            self._logger.info("stopped all runners")
            self.running.clear() 
Example 18
Project: stackhut   Author: nstack   File: runner.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, backend, hutcfg):
        log.debug('Starting Service Runner')
        self.backend = backend
        self.hutcfg = hutcfg
        # select the stack
        self.shim_cmd = shim_cmds.get(self.hutcfg.stack)
        if self.shim_cmd is None:
            raise RuntimeError("Unknown stack - {}".format(self.hutcfg.stack))

        # init the local runtime service
        self.runtime_server = RuntimeServer(backend)
        # init the rpc server
        self.rpc = rpc.StackHutRPC(self.backend, self.shim_cmd)

        assert threading.current_thread() == threading.main_thread()
        signal.signal(signal.SIGTERM, sigterm_handler)
        signal.signal(signal.SIGINT, sigterm_handler) 
Example 19
Project: teek   Author: Akuli   File: _tcl_calls.py    MIT License 6 votes vote down vote up
def __init__(self):
        assert threading.current_thread() is threading.main_thread()
        # get_ident() is faster than threading.current_thread(), so that is
        # used elsewhere in the performance-critical stuff
        self._main_thread_ident = threading.get_ident()

        self._init_threads_called = False

        # tkinter does this :D i have no idea what each argument means
        self._app = _tkinter.create(None, sys.argv[0], 'Tk', 1, 1, 1, 0, None)

        self._app.call('wm', 'withdraw', '.')
        self._app.call('package', 'require', 'Ttk')

        # when a main-thread-needing function is called from another thread, a
        # tuple like this is added to this queue:
        #
        #    (func, args, kwargs, future)
        #
        # func is a function that MUST be called from main thread
        # args and kwargs are arguments for func
        # future will be set when the function has been called
        #
        # the function is called from Tk's event loop
        self._call_queue = queue.Queue() 
Example 20
Project: teek   Author: Akuli   File: test_threads.py    MIT License 6 votes vote down vote up
def test_make_thread_safe(handy_callback, deinit_threads):
    @teek.make_thread_safe
    @handy_callback
    def thread_target():
        assert threading.current_thread() is threading.main_thread()

    teek.init_threads()
    thread = threading.Thread(target=thread_target)
    thread.start()

    # make_thread_safe needs teek.run to work
    teek.after(500, teek.quit)
    teek.run()

    assert not thread.is_alive()
    assert thread_target.ran_once() 
Example 21
Project: research   Author: diplomacy   File: process.py    MIT License 6 votes vote down vote up
def kill_subprocesses_on_exit():
    """ Registers a function to kill the subprocesses automatically on exit """
    def on_exit():
        """ Cleanup for sys.exit() """
        LOGGER.info('sys.exit() called. Sending SIG_KILL to all processes in process group.')
        os.killpg(0, signal.SIGKILL)

    def on_sig_term(*args):
        """ Cleanup for SIGTERM """
        del args
        time.sleep(10.)
        sys.exit(0)

    # Registering handlers for proper termination
    os.setpgrp()
    atexit.register(on_exit)
    if threading.current_thread() == threading.main_thread():
        signal.signal(signal.SIGTERM, on_sig_term)


# ==== Run Tests in Separate Process ==== 
Example 22
Project: sisyphus   Author: rwth-i6   File: tools.py    Mozilla Public License 2.0 6 votes vote down vote up
def default_handle_exception_interrupt_main_thread(func):
    """
    :param func: any function. usually run in another thread.
      If some exception occurs, it will interrupt the main thread (send KeyboardInterrupt to the main thread).
      If this is run in the main thread itself, it will raise SystemExit(1).
    :return: function func wrapped
    """
    import sys
    import _thread
    import threading

    def wrapped_func(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception:
            logging.error("Exception in thread %r:" % threading.current_thread())
            sys.excepthook(*sys.exc_info())
            if threading.current_thread() is not threading.main_thread():
                _thread.interrupt_main()
            raise SystemExit(1)

    return wrapped_func 
Example 23
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork(self):
        code = """if 1:
            import os, threading

            pid = os.fork()
            if pid == 0:
                main = threading.main_thread()
                print(main.name)
                print(main.ident == threading.current_thread().ident)
                print(main.ident == threading.get_ident())
            else:
                os.waitpid(pid, 0)
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "MainThread\nTrue\nTrue\n") 
Example 24
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_main_thread_after_fork_from_nonmain_thread(self):
        code = """if 1:
            import os, threading, sys

            def f():
                pid = os.fork()
                if pid == 0:
                    main = threading.main_thread()
                    print(main.name)
                    print(main.ident == threading.current_thread().ident)
                    print(main.ident == threading.get_ident())
                    # stdout is fully buffered because not a tty,
                    # we have to flush before exit.
                    sys.stdout.flush()
                else:
                    os.waitpid(pid, 0)

            th = threading.Thread(target=f)
            th.start()
            th.join()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode().replace('\r', '')
        self.assertEqual(err, b"")
        self.assertEqual(data, "Thread-1\nTrue\nTrue\n") 
Example 25
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_threading.py    GNU General Public License v3.0 6 votes vote down vote up
def test_3_join_in_forked_from_thread(self):
        # Like the test above, but fork() was called from a worker thread
        # In the forked process, the main Thread object must be marked as stopped.

        script = """if 1:
            main_thread = threading.current_thread()
            def worker():
                childpid = os.fork()
                if childpid != 0:
                    os.waitpid(childpid, 0)
                    sys.exit(0)

                t = threading.Thread(target=joiningfunc,
                                     args=(main_thread,))
                print('end of main')
                t.start()
                t.join() # Should not block: main_thread is already stopped

            w = threading.Thread(target=worker)
            w.start()
            """
        self._run_and_join(script) 
Example 26
Project: orange3-bioinformatics   Author: biolab   File: OWGeneSetEnrichment.py    GNU General Public License v3.0 6 votes vote down vote up
def _init_gene_sets_finished(self, f):
        assert self.thread() is QThread.currentThread()
        assert threading.current_thread() == threading.main_thread()
        assert self._task is not None
        assert self._task.future is f
        assert f.done()

        self._task = None
        self.progress_bar.finish()
        self.setStatusMessage('')

        try:
            results = f.result()  # type: list
            [self.data_model.appendRow(model_item) for model_item in results]
            self.filter_proxy_model.setSourceModel(self.data_model)
            self.data_view.selectionModel().selectionChanged.connect(self.commit)
            self._update_fdr()
            self.filter_data_view()
            self.set_selection()
            self.update_info_box()

        except Exception as ex:
            print(ex) 
Example 27
Project: orange3-bioinformatics   Author: biolab   File: OWGeneSets.py    GNU General Public License v3.0 6 votes vote down vote up
def _init_gene_sets_finished(self, f):
        assert self.thread() is QThread.currentThread()
        assert threading.current_thread() == threading.main_thread()
        assert self._task is not None
        assert self._task.future is f
        assert f.done()

        self._task = None
        self.progress_bar.finish()
        self.setStatusMessage('')

        try:
            results = f.result()  # type: list
            [self.data_model.appendRow(model_item) for model_item in results]
            self.filter_proxy_model.setSourceModel(self.data_model)
            self.data_view.selectionModel().selectionChanged.connect(self.commit)
            self.filter_data_view()
            self.set_selection()
            self.update_info_box()
        except Exception as ex:
            print(ex) 
Example 28
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: currentThread.py    MIT License 5 votes vote down vote up
def myChildThread():
  print("Child Thread Starting")
  time.sleep(5)
  print("Current Thread ----------")
  print(threading.current_thread())
  print("-------------------------")
  print("Main Thread -------------")
  print(threading.main_thread())
  print("-------------------------")
  print("Child Thread Ending") 
Example 29
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: mainThread.py    MIT License 5 votes vote down vote up
def myChildThread():
  print("Child Thread Starting")
  time.sleep(5)
  print("Current Thread ----------")
  print(threading.current_thread())
  print("-------------------------")
  print("Main Thread -------------")
  print(threading.main_thread())
  print("-------------------------")
  print("Child Thread Ending") 
Example 30
Project: dataflow   Author: tensorpack   File: concurrency.py    Apache License 2.0 5 votes vote down vote up
def is_main_thread():
    if six.PY2:
        return isinstance(threading.current_thread(), threading._MainThread)
    else:
        # a nicer solution with py3
        return threading.current_thread() == threading.main_thread() 
Example 31
Project: vscode-mayapy   Author: FXTD-ODYSSEY   File: attach_script.py    MIT License 5 votes vote down vote up
def get_main_thread_instance(threading):
    if hasattr(threading, 'main_thread'):
        return threading.main_thread()
    else:
        # On Python 2 we don't really have an API to get the main thread,
        # so, we just get it from the 'shutdown' bound method.
        return threading._shutdown.im_self 
Example 32
Project: Expy   Author: ray306   File: shared.py    GNU General Public License v3.0 5 votes vote down vote up
def serial_port_listener():
    # main_thread = threading.main_thread()
    global check_serial_port  # serial port listener
    global serial_port_state
    global start_tp
    while 1:
        time.sleep(0.0005)
        if check_serial_port:
            serial_port_state = ser.read()
            start_tp = time.time()
            check_serial_port = False 
Example 33
Project: NiujiaoDebugger   Author: MrSrc   File: test_threading.py    GNU General Public License v3.0 5 votes vote down vote up
def test_main_thread(self):
        main = threading.main_thread()
        self.assertEqual(main.name, 'MainThread')
        self.assertEqual(main.ident, threading.current_thread().ident)
        self.assertEqual(main.ident, threading.get_ident())

        def f():
            self.assertNotEqual(threading.main_thread().ident,
                                threading.current_thread().ident)
        th = threading.Thread(target=f)
        th.start()
        th.join() 
Example 34
Project: NiujiaoDebugger   Author: MrSrc   File: test_threading.py    GNU General Public License v3.0 5 votes vote down vote up
def test_main_thread_during_shutdown(self):
        # bpo-31516: current_thread() should still point to the main thread
        # at shutdown
        code = """if 1:
            import gc, threading

            main_thread = threading.current_thread()
            assert main_thread is threading.main_thread()  # sanity check

            class RefCycle:
                def __init__(self):
                    self.cycle = self

                def __del__(self):
                    print("GC:",
                          threading.current_thread() is main_thread,
                          threading.main_thread() is main_thread,
                          threading.enumerate() == [main_thread])

            RefCycle()
            gc.collect()  # sanity check
            x = RefCycle()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode()
        self.assertEqual(err, b"")
        self.assertEqual(data.splitlines(),
                         ["GC: True True True"] * 2) 
Example 35
Project: tox   Author: tox-dev   File: stdlib.py    MIT License 5 votes vote down vote up
def is_main_thread():
    """returns true if we are within the main thread"""
    cur_thread = threading.current_thread()
    if sys.version_info >= (3, 4):
        return cur_thread is threading.main_thread()
    else:
        # noinspection PyUnresolvedReferences
        return isinstance(cur_thread, threading._MainThread)


# noinspection PyPep8Naming 
Example 36
Project: moler   Author: nokia   File: connection_observer.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def inside_main_thread():
        in_main_thread = threading.current_thread() is threading.main_thread()
        return in_main_thread 
Example 37
Project: mongomotor   Author: jucacrispim   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def is_main_thread():
    return threading.current_thread() == threading.main_thread() 
Example 38
Project: tensorpack-mask-rcnn   Author: armandmcqueen   File: concurrency.py    Apache License 2.0 5 votes vote down vote up
def is_main_thread():
    if six.PY2:
        return isinstance(threading.current_thread(), threading._MainThread)
    else:
        # a nicer solution with py3
        return threading.current_thread() == threading.main_thread() 
Example 39
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threading.py    GNU General Public License v3.0 5 votes vote down vote up
def test_main_thread(self):
        main = threading.main_thread()
        self.assertEqual(main.name, 'MainThread')
        self.assertEqual(main.ident, threading.current_thread().ident)
        self.assertEqual(main.ident, threading.get_ident())

        def f():
            self.assertNotEqual(threading.main_thread().ident,
                                threading.current_thread().ident)
        th = threading.Thread(target=f)
        th.start()
        th.join() 
Example 40
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_ki.py    GNU General Public License v2.0 5 votes vote down vote up
def test_ki_with_broken_threads():
    thread = threading.main_thread()

    # scary!
    original = threading._active[thread.ident]

    # put this in a try finally so we don't have a chance of cascading a
    # breakage down to everything else
    try:
        del threading._active[thread.ident]

        @_core.enable_ki_protection
        async def inner():
            assert signal.getsignal(
                signal.SIGINT
            ) != signal.default_int_handler

        _core.run(inner)
    finally:
        threading._active[thread.ident] = original


# For details on why this test is non-trivial, see:
#   https://github.com/python-trio/trio/issues/42
#   https://github.com/python-trio/trio/issues/109
# To make it an even better test, we should try doing
#   pthread_kill(pthread_self, SIGINT)
# in the child thread, to make sure signals in non-main threads also wake up
# the main loop... but currently that test would fail (see gh-109 again). 
Example 41
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threading.py    GNU General Public License v2.0 5 votes vote down vote up
def test_main_thread(self):
        main = threading.main_thread()
        self.assertEqual(main.name, 'MainThread')
        self.assertEqual(main.ident, threading.current_thread().ident)
        self.assertEqual(main.ident, threading.get_ident())

        def f():
            self.assertNotEqual(threading.main_thread().ident,
                                threading.current_thread().ident)
        th = threading.Thread(target=f)
        th.start()
        th.join() 
Example 42
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threading.py    GNU General Public License v2.0 5 votes vote down vote up
def test_main_thread_during_shutdown(self):
        # bpo-31516: current_thread() should still point to the main thread
        # at shutdown
        code = """if 1:
            import gc, threading

            main_thread = threading.current_thread()
            assert main_thread is threading.main_thread()  # sanity check

            class RefCycle:
                def __init__(self):
                    self.cycle = self

                def __del__(self):
                    print("GC:",
                          threading.current_thread() is main_thread,
                          threading.main_thread() is main_thread,
                          threading.enumerate() == [main_thread])

            RefCycle()
            gc.collect()  # sanity check
            x = RefCycle()
        """
        _, out, err = assert_python_ok("-c", code)
        data = out.decode()
        self.assertEqual(err, b"")
        self.assertEqual(data.splitlines(),
                         ["GC: True True True"] * 2) 
Example 43
Project: TFTHelper   Author: AliMZaini   File: test__threading_native_before_monkey.py    MIT License 5 votes vote down vote up
def test_main_thread(self):
        current = threading.current_thread()
        self.assertFalse(isinstance(current, threading._DummyThread))
        self.assertTrue(isinstance(current, monkey.get_original('threading', 'Thread')))
        # in 3.4, if the patch is incorrectly done, getting the repr
        # of the thread fails
        repr(current)

        if hasattr(threading, 'main_thread'): # py 3.4
            self.assertEqual(threading.current_thread(), threading.main_thread()) 
Example 44
Project: TFTHelper   Author: AliMZaini   File: threading.py    MIT License 5 votes vote down vote up
def main_native_thread():
        return __threading__.main_thread() # pylint:disable=no-member 
Example 45
Project: TFTHelper   Author: AliMZaini   File: threading.py    MIT License 5 votes vote down vote up
def _gevent_will_monkey_patch(native_module, items, warn): # pylint:disable=unused-argument
    # Make sure the MainThread can be found by our current greenlet ID,
    # otherwise we get a new DummyThread, which cannot be joined.
    # Fixes tests in test_threading_2 under PyPy.
    main_thread = main_native_thread()
    if __threading__.current_thread() != main_thread:
        warn("Monkey-patching outside the main native thread. Some APIs "
             "will not be available. Expect a KeyError to be printed at shutdown.")
        return

    if _get_ident() not in __threading__._active:
        main_id = main_thread.ident
        del __threading__._active[main_id]
        main_thread._ident = main_thread._Thread__ident = _get_ident()
        __threading__._active[_get_ident()] = main_thread 
Example 46
Project: Raid-Toolbox   Author: DeadBread76   File: serversmasherGUI.py    GNU General Public License v2.0 5 votes vote down vote up
def heartbeat(interval):
    ack = {
            "op": 1,
            "d": None
        }
    while threading.main_thread().is_alive():
        time.sleep(interval/1000)
        try:
            ws.send(json.dumps(ack))
        except Exception:
            break 
Example 47
Project: CrypTen   Author: facebookresearch   File: multithread_test_case.py    MIT License 5 votes vote down vote up
def rank(self):
        from crypten.communicator import InProcessCommunicator

        if threading.current_thread() == threading.main_thread():
            return self.MAIN_PROCESS_RANK

        return InProcessCommunicator.get().rank 
Example 48
Project: CrypTen   Author: facebookresearch   File: multithread_test_case.py    MIT License 5 votes vote down vote up
def join_or_run(fn):
        @wraps(fn)
        def wrapper(self):
            if threading.current_thread() == threading.main_thread():
                self._join_threads()
            else:
                fn(self)

        return wrapper 
Example 49
Project: CrypTen   Author: facebookresearch   File: multithread_test_case.py    MIT License 5 votes vote down vote up
def setUp(self):
        super().setUp()

        if threading.current_thread() != threading.main_thread():
            return
        test_name = self._current_test_name()
        test_fn = getattr(self, test_name)
        self.exception_queue = queue.Queue()
        self.threads = [
            Thread(target=self._run, args=(test_fn, rank, self.world_size))
            for rank in range(self.world_size)
        ]
        for t in self.threads:
            t.start() 
Example 50
Project: py12306   Author: amikey   File: func.py    Apache License 2.0 5 votes vote down vote up
def is_main_thread():
    return threading.current_thread() == threading.main_thread() 
Example 51
Project: automlbenchmark   Author: openml   File: process.py    MIT License 5 votes vote down vote up
def get_thread(tid=None):
    return threading.current_thread() if tid is None \
        else threading.main_thread() if tid == 0 \
        else next(filter(lambda t: t.ident == tid, threading.enumerate())) 
Example 52
Project: garage   Author: clchiou   File: signals.py    MIT License 5 votes vote down vote up
def __enter__(self):
        # ``set_wakeup_fd`` can only be called from the main thread.
        ASSERT.is_(threading.current_thread(), threading.main_thread())
        # Disallow nested use; ``SignalSource`` is a singleton and is
        # intended to be used as such.
        ASSERT.none(self._wakeup_fd)
        sock_r, self._sock_w = socket.socketpair()
        self._sock_r = adapters.SocketAdapter(sock_r)
        self._sock_w.setblocking(False)
        self._wakeup_fd = signal.set_wakeup_fd(self._sock_w.fileno())
        return self 
Example 53
Project: garage   Author: clchiou   File: signals.py    MIT License 5 votes vote down vote up
def restore_wakeup_fd(restore_fd, expect_fd):

    if threading.get_ident() != threading.main_thread().ident:
        LOG.error(
            'cannot restore signal wakeup fd in non-main thread: fd=%d',
            restore_fd,
        )
        return

    last_fd = signal.set_wakeup_fd(restore_fd)
    if last_fd != expect_fd:
        LOG.error(
            'expect last signal wakeup fd to be %d, not %d',
            expect_fd, last_fd,
        ) 
Example 54
Project: baboossh   Author: cybiere   File: params.py    GNU General Public License v3.0 5 votes vote down vote up
def get(cls):
        mainThreadName = threading.main_thread().getName()
        currentName = threading.currentThread().getName()
        if currentName != mainThreadName:
            if currentName in cls.__threadsConn.keys():
                return cls.__threadsConn[currentName]
            else:
                cls.connect(cls.__workspace)
                return cls.__threadsConn[currentName]
        if cls.__conn is None:
            raise ValueError("Trying to use unconnected database")
        return cls.__conn 
Example 55
Project: baboossh   Author: cybiere   File: params.py    GNU General Public License v3.0 5 votes vote down vote up
def close(cls):
        mainThreadName = threading.main_thread().getName()
        currentName = threading.currentThread().getName()
        if currentName != mainThreadName:
            if currentName in cls.__threadsConn.keys():
                cls.__threadsConn[currentName].close()
                del cls.__threadsConn[currentName]
            return
        cls.__conn.close()
        cls.__conn = None 
Example 56
Project: lexpredict-contraxsuite   Author: LexPredict   File: debug_utils.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def listen():
    import threading

    if settings.DEBUG_STACK_DUMP_ENABLED and threading.current_thread() is threading.main_thread():
        signal.signal(settings.DEBUG_STACK_DUMP_SIGNAL, print_stack_traces) 
Example 57
Project: FeelUOwn   Author: feeluown   File: player.py    GNU General Public License v3.0 5 votes vote down vote up
def call_soon(func, loop):
    if threading.current_thread() is threading.main_thread():
        func()
    else:
        loop.call_soon_threadsafe(func) 
Example 58
Project: FeelUOwn   Author: feeluown   File: task.py    GNU General Public License v3.0 5 votes vote down vote up
def is_in_loop_thread():
    """check if current thread has event loop"""
    return threading.current_thread() is threading.main_thread() 
Example 59
Project: RealtimePythonChat   Author: quangtqag   File: threading.py    MIT License 5 votes vote down vote up
def main_native_thread():
        return __threading__.main_thread() # pylint:disable=no-member 
Example 60
Project: FluffyCogs   Author: zephyrkul   File: heartattack.py    GNU General Public License v3.0 5 votes vote down vote up
def emit(self, record):
        stack = sys._current_frames()[threading.main_thread().ident]
        stack = traceback.format_stack(stack)
        ha_log.warning(
            f"{record.msg}\nMain thread stack (most recent call last):\n%s",
            *record.args,
            "".join(stack),
        ) 
Example 61
Project: OdooQuant   Author: haogefeifei   File: httrader.py    GNU General Public License v3.0 5 votes vote down vote up
def remove_heart_log(*args, **kwargs):
    if six.PY2:
        if threading.current_thread().name == 'MainThread':
            debug_log(*args, **kwargs)
    else:
        if threading.current_thread() == threading.main_thread():
            debug_log(*args, **kwargs) 
Example 62
Project: zproc   Author: scientifichackers   File: util.py    MIT License 5 votes vote down vote up
def is_main_thread() -> bool:
    return threading.current_thread() == threading.main_thread() 
Example 63
Project: teek   Author: Akuli   File: _tcl_calls.py    MIT License 5 votes vote down vote up
def _get_interp():
    global _interp

    if _interp is None:
        if threading.current_thread() is not threading.main_thread():
            raise RuntimeError("init_threads() wasn't called")
        _interp = _TclInterpreter()
    return _interp 
Example 64
Project: teek   Author: Akuli   File: _tcl_calls.py    MIT License 5 votes vote down vote up
def quit():
    """Stop the event loop and destroy all widgets.

    This function calls ``destroy .`` in Tcl, and that's documented in
    :man:`destroy(3tk)`. Note that this function does not tell Python to quit;
    only teek quits, so you can do this::

        import teek

        window = teek.Window()
        teek.Button(window, "Quit", teek.quit).pack()
        teek.run()
        print("Still alive")

    If you click the button, it interrupts ``teek.run()`` and the print runs.
    """
    global _interp

    if threading.current_thread() is not threading.main_thread():
        # TODO: allow quitting from other threads or document this
        raise RuntimeError("can only quit from main thread")

    if _interp is not None:
        teek.before_quit.run()
        _interp.call('destroy', '.')

        # to avoid a weird errors, see test_weird_error in test_tcl_calls.py
        for command in teek.tcl_call([str], 'info', 'commands'):
            if command.startswith('teek_command_'):
                delete_command(command)

        _interp = None
        teek.after_quit.run() 
Example 65
Project: Learning-Concurrency-in-Python-Video-   Author: PacktPublishing   File: currentThread.py    MIT License 5 votes vote down vote up
def myChildThread():
  print("Child Thread Starting")
  time.sleep(5)
  print("Current Thread ----------")
  print(threading.current_thread())
  print("-------------------------")
  print("Main Thread -------------")
  print(threading.main_thread())
  print("-------------------------")
  print("Child Thread Ending") 
Example 66
Project: Learning-Concurrency-in-Python-Video-   Author: PacktPublishing   File: mainThread.py    MIT License 5 votes vote down vote up
def myChildThread():
  print("Child Thread Starting")
  time.sleep(5)
  print("Current Thread ----------")
  print(threading.current_thread())
  print("-------------------------")
  print("Main Thread -------------")
  print(threading.main_thread())
  print("-------------------------")
  print("Child Thread Ending") 
Example 67
Project: r5-2019   Author: ut-ras   File: robot.py    MIT License 5 votes vote down vote up
def run(self):
        """
        Runs the robot instruction sequence.
        """

        self.init()

        # Rolling buffer for framerate computation
        frame_times = []

        # Check for main thread alive and quit flag not set
        while threading.main_thread().is_alive() and not self.done:

            # Init timer
            if len(frame_times) > self.frame_avg:
                frame_times.pop(0)
            start = time.time()

            self.loop()

            # Average fps
            frame_times.append(time.time() - start)
            self.fps = 1. * len(frame_times) / sum(frame_times)

        # Set semaphores
        self.done = True
        self.busy = True 
Example 68
Project: r5-2019   Author: ut-ras   File: robotframe.py    MIT License 5 votes vote down vote up
def run(self):
        """
        Runs the robot instruction sequence.
        """

        self.init()

        # Rolling buffer for framerate computation
        frame_times = []

        # Check for main thread alive and quit flag not set
        while threading.main_thread().is_alive() and not self.done:

            # Init timer
            if len(frame_times) > self.frame_avg:
                frame_times.pop(0)
            start = time.time()

            self.loop()

            # Average fps
            frame_times.append(time.time() - start)
            frame_times_sum = sum(frame_times)
            if frame_times_sum != 0:
                self.fps = 1. * len(frame_times) / frame_times_sum

        # Set semaphores
        self.done = True
        self.busy = True 
Example 69
Project: r5-2019   Author: ut-ras   File: vision_thread.py    MIT License 5 votes vote down vote up
def run(self):
        while threading.main_thread().is_alive() and not self.done:
            if self.capture:
                img = self.camera.capture()

                self.led.on()
                self.objects = self.vision.process(img)
                self.flag = True
                self.led.off()
            else:
                time.sleep(0.1) 
Example 70
Project: dotfiles   Author: xbaysal11   File: attach_script.py    GNU General Public License v3.0 5 votes vote down vote up
def get_main_thread_instance(threading):
    if hasattr(threading, 'main_thread'):
        return threading.main_thread()
    else:
        # On Python 2 we don't really have an API to get the main thread,
        # so, we just get it from the 'shutdown' bound method.
        return threading._shutdown.im_self 
Example 71
Project: gtui   Author: CtheSky   File: executor.py    GNU General Public License v3.0 5 votes vote down vote up
def get_main_thread_log_records(self):
        return self.name2records.get(threading.main_thread().name, []) 
Example 72
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_threading.py    GNU General Public License v3.0 5 votes vote down vote up
def test_main_thread(self):
        main = threading.main_thread()
        self.assertEqual(main.name, 'MainThread')
        self.assertEqual(main.ident, threading.current_thread().ident)
        self.assertEqual(main.ident, threading.get_ident())

        def f():
            self.assertNotEqual(threading.main_thread().ident,
                                threading.current_thread().ident)
        th = threading.Thread(target=f)
        th.start()
        th.join() 
Example 73
Project: orange3-bioinformatics   Author: biolab   File: OWdictyExpress.py    GNU General Public License v3.0 5 votes vote down vote up
def progress_advance(self):
        # GUI should be updated in main thread. That's why we are calling advance method here
        assert threading.current_thread() == threading.main_thread()
        if self.progress_bar:
            self.progress_bar.advance() 
Example 74
Project: zmirror   Author: aploium   File: zmirror.py    MIT License 4 votes vote down vote up
def cron_task_container(task_dict, add_task_only=False):
    """
    定时任务容器. 调用目标函数, 并在运行结束后创建下一次定时

    :param task_dict: 定时任务的相关参数, dict
      { "target":目标函数(可调用的函数对象,不是函数名字符串) 必须,
        "iterval":任务延时(秒) 可选,
        "priority":优先级 可选,
        "name":定时任务别名 可选
        "args":位置型参数 (arg1,arg2) 可选,
        "kwargs":键值型参数 {key:value,} 可选,
      }
    :param add_task_only: 是否只添加定时任务而不执行
    """
    global task_scheduler
    if not add_task_only:
        # 执行任务
        try:
            infoprint('CronTask:', task_dict.get('name', str(task_dict['target'])), 'Target:', str(task_dict['target']))

            target_func = task_dict.get('target')
            if target_func is None:
                raise ValueError("target is not given in " + str(task_dict))
            target_func(
                *(task_dict.get('args', ())),  # 解开参数以后传递
                **(task_dict.get('kwargs', {}))
            )
        except:  # coverage: exclude
            errprint('ErrorWhenProcessingCronTasks', task_dict)
            traceback.print_exc()

    # 当全局开关关闭时, 自动退出线程
    if not enable_cron_tasks:
        if threading.current_thread() != threading.main_thread():
            exit()
        else:
            return

    # 添加下一次定时任务
    task_scheduler.enter(
        task_dict.get('interval', 300),
        task_dict.get('priority', 999),
        cron_task_container,
        (task_dict,)
    ) 
Example 75
Project: TFTHelper   Author: AliMZaini   File: test__threading_monkey_in_thread.py    MIT License 4 votes vote down vote up
def test_patch_in_thread(self):
        all_warnings = []
        try:
            get_ident = threading.get_ident
        except AttributeError:
            get_ident = threading._get_ident

        def process_warnings(warnings):
            all_warnings.extend(warnings)
        monkey._process_warnings = process_warnings

        current = threading.current_thread()
        current_id = get_ident()

        def target():
            tcurrent = threading.current_thread()
            monkey.patch_all()
            tcurrent2 = threading.current_thread()
            self.assertIsNot(tcurrent, current)
            # We get a dummy thread now
            self.assertIsNot(tcurrent, tcurrent2)

        thread = threading.Thread(target=target)
        thread.start()
        try:
            thread.join()
        except: # pylint:disable=bare-except
            # XXX: This can raise LoopExit in some cases.
            greentest.reraiseFlakyTestRaceCondition()

        self.assertNotIsInstance(current, threading._DummyThread)
        self.assertIsInstance(current, monkey.get_original('threading', 'Thread'))


        # We generated some warnings
        if sys.version_info >= (3, 4):
            self.assertEqual(all_warnings,
                             ['Monkey-patching outside the main native thread. Some APIs will not be '
                              'available. Expect a KeyError to be printed at shutdown.',
                              'Monkey-patching not on the main thread; threading.main_thread().join() '
                              'will hang from a greenlet'])
        else:
            self.assertEqual(all_warnings,
                             ['Monkey-patching outside the main native thread. Some APIs will not be '
                              'available. Expect a KeyError to be printed at shutdown.'])


        # Manual clean up so we don't get a KeyError
        del threading._active[current_id]
        threading._active[(getattr(threading, 'get_ident', None) or threading._get_ident)()] = current 
Example 76
Project: zmirror   Author: ttestdock   File: zmirror.py    MIT License 4 votes vote down vote up
def cron_task_container(task_dict, add_task_only=False):
    """
    定时任务容器. 调用目标函数, 并在运行结束后创建下一次定时

    :param task_dict: 定时任务的相关参数, dict
      { "target":目标函数(可调用的函数对象,不是函数名字符串) 必须,
        "iterval":任务延时(秒) 可选,
        "priority":优先级 可选,
        "name":定时任务别名 可选
        "args":位置型参数 (arg1,arg2) 可选,
        "kwargs":键值型参数 {key:value,} 可选,
      }
    :param add_task_only: 是否只添加定时任务而不执行
    """
    global task_scheduler
    if not add_task_only:
        # 执行任务
        try:
            infoprint('CronTask:', task_dict.get('name', str(task_dict['target'])), 'Target:', str(task_dict['target']))

            target_func = task_dict.get('target')
            if target_func is None:
                raise ValueError("target is not given in " + str(task_dict))
            target_func(
                *(task_dict.get('args', ())),  # 解开参数以后传递
                **(task_dict.get('kwargs', {}))
            )
        except:  # coverage: exclude
            errprint('ErrorWhenProcessingCronTasks', task_dict)
            traceback.print_exc()

    # 当全局开关关闭时, 自动退出线程
    if not enable_cron_tasks:
        if threading.current_thread() != threading.main_thread():
            exit()
        else:
            return

    # 添加下一次定时任务
    task_scheduler.enter(
        task_dict.get('interval', 300),
        task_dict.get('priority', 999),
        cron_task_container,
        (task_dict,)
    ) 
Example 77
Project: photonLauncher   Author: sunjerry019   File: guimicro.py    Apache License 2.0 4 votes vote down vote up
def KeyboardInterruptHandler(self, signal = None, frame = None, abortTrigger = False):
        # 2 args above for use with signal.signal
        # ALt + B also aborts

        # Disable the abort button
        self._abortBtn.setEnabled(False)
        self._abortBtn.setStyleSheet("background-color: #ccc;")

        # Close shutter
        self.stageControl.controller.shutter.quietLog = True
        if not abortTrigger:
            self.setOperationStatus("^C Detected: Aborting the FIFO stack. Shutter will be closed as part of the aborting process.")
        else:
            self.setOperationStatus("Aborting the FIFO stack. Shutter will be closed as part of the aborting process.")
        self.stageControl.controller.shutter.close()
        self.stageControl.controller.shutter.quietLog = False
        # / Close shutter

        # End all running threads
        main_thread = threading.main_thread()
        for p in threading.enumerate():
            if p is main_thread:
                continue

            if isinstance(p, ThreadWithExc):
                p.terminate()
        # / End all running threads

        if not self.devMode:
            self.stageControl.controller.abort()

        self._SR_start.setEnabled(True)
        self._AR_start.setEnabled(True)

        # Enable the abort button
        self._abortBtn.setStyleSheet("background-color: #DF2928;")
        self._abortBtn.setEnabled(True)

        # self.dev.close()
        # print("Exiting")
        # sys.exit(1)
        # use os._exit(1) to avoid raising any SystemExit exception 
Example 78
Project: garage   Author: clchiou   File: signals.py    MIT License 4 votes vote down vote up
def __init__(self, capacity=None):

        current_thread = threading.current_thread()
        ASSERT(
            current_thread.ident == threading.main_thread().ident,
            'expect signal queue be initialized in the main thread, not %r',
            current_thread,
        )

        if capacity is None:
            capacity = self.CAPACITY

        stack = contextlib.ExitStack()
        try:

            self._queue = queues.Queue(capacity=capacity)
            stack.callback(self._queue.close)

            rfd, wfd = os.pipe2(os.O_CLOEXEC)
            stack.callback(os.close, rfd)
            stack.callback(os.close, wfd)

            os.set_blocking(wfd, False)

            last_fd = signal.set_wakeup_fd(wfd)
            stack.callback(restore_wakeup_fd, last_fd, wfd)

            ASSERT(
                last_fd == -1,
                'expect no signal wakeup fd being set: %d', last_fd,
            )

            thread = threading.Thread(
                target=receive_signals,
                name=self.THREAD_NAME,
                args=(rfd, self._queue),
                daemon=True,
            )
            thread.start()
            utils.set_pthread_name(thread, self.THREAD_NAME)

        except Exception:
            stack.close()
            raise

        self._stack = stack 
Example 79
Project: bibcat   Author: KnowledgeLinks   File: loc_subjects.py    MIT License 4 votes vote down vote up
def _index_subjects(self):
        """ quereies the triplestore for all subject uri"""

        lg = logging.getLogger("%s.%s" % (self.ln, inspect.stack()[0][3]))
        lg.setLevel(self.log_level)

        # if the subjects have been indexed and there are no new subjects exit
        if self.data_status.get("indexed") and not self.new_subjects:
            return
        # get a list of all the loc_subject URIs
        sparql = """
            SELECT ?s 
            {
                ?s skos:inScheme <http://id.loc.gov/authorities/subjects> .
            }"""
        results = run_sparql_query(sparql=sparql)
        # Start processing through
        self.time_start = datetime.datetime.now()
        batch_size = 12000
        if len(results) > batch_size:
            batch_end = batch_size
        else:
            batch_end = len(results) - 1
        batch_start = 0
        batch_num = 1
        self.batch_data = {}
        self.batch_data[batch_num] = []
        end = False
        last = False
        while not end:
            lg.debug("batch %s: %s-%s", batch_num, batch_start, batch_end)
            for i, subj in enumerate(results[batch_start:batch_end]):
                th = threading.Thread(name=batch_start + i + 1,
                                      target=self._index_subject_item,
                                      args=(iri(subj['s']['value']),
                                            i+1,batch_num,))
                th.start()
                #self._index_subject_item(iri(subj['s']['value']),i+1)
            print(datetime.datetime.now() - self.time_start)
            main_thread = threading.main_thread()
            for t in threading.enumerate():
                if t is main_thread:
                    continue
                #print('joining %s', t.getName())
                t.join()
            action_list = \
                    self.es_worker.make_action_list(self.batch_data[batch_num])
            self.es_worker.bulk_save(action_list)
            del self.batch_data[batch_num]
            batch_end += batch_size
            batch_start += batch_size
            if last:
                end = True
            if len(results) <= batch_end:
                batch_end = len(results)
                last = True
            batch_num += 1
            self.batch_data[batch_num] = []
            print(datetime.datetime.now() - self.time_start) 
Example 80
Project: variety   Author: varietywalls   File: Util.py    GNU General Public License v3.0 4 votes vote down vote up
def profiler(self, frame, event, arg):
        filename = frame.f_code.co_filename

        tid = threading.get_ident()

        if not self.is_target_path(filename):
            if tid not in self.nontarget_depths:
                # Pick up where the main thread left off
                self.nontarget_depths[tid] = self.nontarget_depths.get(
                    threading.main_thread().ident, 1
                )
            else:
                self.nontarget_depths[tid] += 1
        else:
            self.nontarget_depths[tid] = 0

        tname = threading.current_thread().name

        if event == "call":
            if self.nontarget_depths[tid] > self.MAX_NONTARGET_DEPTH:
                # Don't log past our max depth for packages that we're not tracking
                return
            else:
                # In order: function name, line number, filename
                s = "[%s] -> Entering function: %s\t(line %s in %s)" % (
                    tname,
                    frame.f_code.co_name,
                    frame.f_lineno,
                    filename,
                )
                if self.nontarget_depths[tid] == self.MAX_NONTARGET_DEPTH:
                    s += (
                        " - not tracing further because MAX_NONTARGET_DEPTH=%s"
                        % self.MAX_NONTARGET_DEPTH
                    )
                logger.debug(s)

        elif event == "return":
            if self.nontarget_depths[tid] > self.MAX_NONTARGET_DEPTH:
                return

            logger.debug(
                "[%s] -> Leaving function:  %s\t(line %s in %s)"
                % (tname, frame.f_code.co_name, frame.f_lineno, filename)
            )