Python threading.get_ident() Examples

The following are code examples for showing how to use threading.get_ident(). 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: webtoon-dl   Author: puilp0502   File: main.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def worker(q):
    tid = threading.get_ident()
    while True:
        job = q.get()
        if job is None or _terminated:
            logger.debug('[%d] Got termination signal; terminating...' % tid)
            q.task_done()
            break
        ep_name, dirname, headers, urls = job  # TODO:Queue structure
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        logger.debug('[%d] Downloading #%s to %s' % (tid, ep_name, dirname))
        counter = 1
        for url in urls:
            resp = requests.get(url, headers=headers)
            path = dirname + '/' + str(counter) + parse_extension(url)
            with open(path, 'wb') as image:
                for chunk in resp.iter_content(1024):
                    image.write(chunk)
            counter = counter + 1
        logger.info('%d/%d downloaded (%s)' % (_total - q.qsize() + 1, _total, ep_name))
        q.task_done() 
Example 2
Project: pyrex   Author: garmin   File: test.py    Apache License 2.0 6 votes vote down vote up
def test_envvars(self):
        conf = self.get_config()
        conf["run"]["envvars"] += " TEST_ENV"
        conf.write_conf()

        test_string = "set_by_test.%d" % threading.get_ident()

        env = os.environ.copy()
        env["TEST_ENV"] = test_string

        s = self.assertPyrexContainerShellCommand(
            "echo $TEST_ENV", env=env, quiet_init=True, capture=True
        )
        self.assertEqual(s, test_string)

        s = self.assertPyrexContainerShellCommand(
            "echo $TEST_ENV2", env=env, quiet_init=True, capture=True
        )
        self.assertEqual(s, "") 
Example 3
Project: NiujiaoDebugger   Author: MrSrc   File: base_events.py    GNU General Public License v3.0 6 votes vote down vote up
def _check_thread(self):
        """Check that the current thread is the thread running the event loop.

        Non-thread-safe methods of this class make this assumption and will
        likely behave incorrectly when the assumption is violated.

        Should only be called when (self._debug == True).  The caller is
        responsible for checking this condition for performance reasons.
        """
        if self._thread_id is None:
            return
        thread_id = threading.get_ident()
        if thread_id != self._thread_id:
            raise RuntimeError(
                "Non-thread-safe operation invoked on an event loop other "
                "than the current one") 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_threaded_import.py    GNU General Public License v3.0 6 votes vote down vote up
def task(N, done, done_tasks, errors):
    try:
        # We don't use modulefinder but still import it in order to stress
        # importing of different modules from several threads.
        if len(done_tasks) % 2:
            import modulefinder
            import random
        else:
            import random
            import modulefinder
        # This will fail if random is not completely initialized
        x = random.randrange(1, 3)
    except Exception as e:
        errors.append(e.with_traceback(None))
    finally:
        done_tasks.append(threading.get_ident())
        finished = len(done_tasks) == N
        if finished:
            done.set() 
Example 5
Project: NiujiaoDebugger   Author: MrSrc   File: test_signal.py    GNU General Public License v3.0 6 votes vote down vote up
def test_pthread_kill(self):
        code = """if 1:
            import signal
            import threading
            import sys

            signum = signal.SIGUSR1

            def handler(signum, frame):
                1/0

            signal.signal(signum, handler)

            tid = threading.get_ident()
            try:
                signal.pthread_kill(tid, signum)
            except ZeroDivisionError:
                pass
            else:
                raise Exception("ZeroDivisionError not raised")
        """
        assert_python_ok('-c', code) 
Example 6
Project: NiujiaoDebugger   Author: MrSrc   File: test_signal.py    GNU General Public License v3.0 6 votes vote down vote up
def test_pthread_kill_main_thread(self):
        # Test that a signal can be sent to the main thread with pthread_kill()
        # before any other thread has been created (see issue #12392).
        code = """if True:
            import threading
            import signal
            import sys

            def handler(signum, frame):
                sys.exit(3)

            signal.signal(signal.SIGUSR1, handler)
            signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
            sys.exit(2)
        """

        with spawn_python('-c', code) as process:
            stdout, stderr = process.communicate()
            exitcode = process.wait()
            if exitcode != 3:
                raise Exception("Child error (exit code %s): %s" %
                                (exitcode, stdout)) 
Example 7
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 8
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 9
Project: NiujiaoDebugger   Author: MrSrc   File: test_capi.py    GNU General Public License v3.0 6 votes vote down vote up
def test_thread_state(self):
        # some extra thread-state tests driven via _testcapi
        def target():
            idents = []

            def callback():
                idents.append(threading.get_ident())

            _testcapi._test_thread_state(callback)
            a = b = callback
            time.sleep(1)
            # Check our main thread is in the list exactly 3 times.
            self.assertEqual(idents.count(threading.get_ident()), 3,
                             "Couldn't find main thread correctly in the list")

        target()
        t = threading.Thread(target=target)
        t.start()
        t.join() 
Example 10
Project: panoptes   Author: yahoo   File: helpers.py    Apache License 2.0 6 votes vote down vote up
def get_os_tid():
    """
    Get the Linux process id associated with the current thread

    Returns:
        int: The process id

    """
    if sys.platform.startswith(u'linux'):
        return ctypes.CDLL(u'libc.so.6').syscall(186)
    else:
        # TODO: This is hacky - we need to replace it with something that actually returns the OS thread ID
        if is_python_2():
            return threading._get_ident()
        else:
            return threading.get_ident() 
Example 11
Project: Blockly-rduino-communication   Author: technologiescollege   File: base_events.py    GNU General Public License v3.0 6 votes vote down vote up
def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
            raise RuntimeError('Event loop is running.')
        self._set_coroutine_wrapper(self._debug)
        self._thread_id = threading.get_ident()
        try:
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = None
            self._set_coroutine_wrapper(False) 
Example 12
Project: Blockly-rduino-communication   Author: technologiescollege   File: base_events.py    GNU General Public License v3.0 6 votes vote down vote up
def _check_thread(self):
        """Check that the current thread is the thread running the event loop.

        Non-thread-safe methods of this class make this assumption and will
        likely behave incorrectly when the assumption is violated.

        Should only be called when (self._debug == True).  The caller is
        responsible for checking this condition for performance reasons.
        """
        if self._thread_id is None:
            return
        thread_id = threading.get_ident()
        if thread_id != self._thread_id:
            raise RuntimeError(
                "Non-thread-safe operation invoked on an event loop other "
                "than the current one") 
Example 13
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threaded_import.py    GNU General Public License v3.0 6 votes vote down vote up
def task(N, done, done_tasks, errors):
    try:
        # We don't use modulefinder but still import it in order to stress
        # importing of different modules from several threads.
        if len(done_tasks) % 2:
            import modulefinder
            import random
        else:
            import random
            import modulefinder
        # This will fail if random is not completely initialized
        x = random.randrange(1, 3)
    except Exception as e:
        errors.append(e.with_traceback(None))
    finally:
        done_tasks.append(threading.get_ident())
        finished = len(done_tasks) == N
        if finished:
            done.set()

# Create a circular import structure: A -> C -> B -> D -> A
# NOTE: `time` is already loaded and therefore doesn't threaten to deadlock. 
Example 14
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_signal.py    GNU General Public License v3.0 6 votes vote down vote up
def test_pthread_kill_main_thread(self):
        # Test that a signal can be sent to the main thread with pthread_kill()
        # before any other thread has been created (see issue #12392).
        code = """if True:
            import threading
            import signal
            import sys

            def handler(signum, frame):
                sys.exit(3)

            signal.signal(signal.SIGUSR1, handler)
            signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
            sys.exit(2)
        """

        with spawn_python('-c', code) as process:
            stdout, stderr = process.communicate()
            exitcode = process.wait()
            if exitcode != 3:
                raise Exception("Child error (exit code %s): %s" %
                                (exitcode, stdout)) 
Example 15
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 16
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 17
Project: midict   Author: ShenggaoZhu   File: __init__.py    MIT License 6 votes vote down vote up
def __repr__(self, _repr_running={}):
        'repr as "MIDict(items, names)"'
        call_key = id(self), _get_ident()
        if call_key in _repr_running: # pragma: no cover
            return '<%s(...)>' % self.__class__.__name__
        _repr_running[call_key] = 1
        try:
            try:
                if self.indices:
                    names = force_list(self.indices.keys())
                    items = force_list(self.items())
                    return '%s(%s, %s)' % (self.__class__.__name__, items, names)
            except AttributeError: # pragma: no cover
                # may not have attr ``indices`` yet
                pass
            return '%s()' % self.__class__.__name__
        finally:
            del _repr_running[call_key] 
Example 18
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: decorators.py    GNU General Public License v3.0 6 votes vote down vote up
def reject_recursive_repeats(to_wrap):
    '''
    Prevent simple cycles by returning None when called recursively with same instance
    '''
    to_wrap.__already_called = {}

    @functools.wraps(to_wrap)
    def wrapped(*args):
        arg_instances = tuple(map(id, args))
        thread_id = threading.get_ident()
        thread_local_args = (thread_id,) + arg_instances
        if thread_local_args in to_wrap.__already_called:
            raise ValueError('Recursively called %s with %r' % (to_wrap, args))
        to_wrap.__already_called[thread_local_args] = True
        try:
            wrapped_val = to_wrap(*args)
        finally:
            del to_wrap.__already_called[thread_local_args]
        return wrapped_val
    return wrapped 
Example 19
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: base_events.py    GNU General Public License v2.0 6 votes vote down vote up
def _check_thread(self):
        """Check that the current thread is the thread running the event loop.

        Non-thread-safe methods of this class make this assumption and will
        likely behave incorrectly when the assumption is violated.

        Should only be called when (self._debug == True).  The caller is
        responsible for checking this condition for performance reasons.
        """
        if self._thread_id is None:
            return
        thread_id = threading.get_ident()
        if thread_id != self._thread_id:
            raise RuntimeError(
                "Non-thread-safe operation invoked on an event loop other "
                "than the current one") 
Example 20
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threaded_import.py    GNU General Public License v2.0 6 votes vote down vote up
def task(N, done, done_tasks, errors):
    try:
        # We don't use modulefinder but still import it in order to stress
        # importing of different modules from several threads.
        if len(done_tasks) % 2:
            import modulefinder
            import random
        else:
            import random
            import modulefinder
        # This will fail if random is not completely initialized
        x = random.randrange(1, 3)
    except Exception as e:
        errors.append(e.with_traceback(None))
    finally:
        done_tasks.append(threading.get_ident())
        finished = len(done_tasks) == N
        if finished:
            done.set() 
Example 21
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_signal.py    GNU General Public License v2.0 6 votes vote down vote up
def test_pthread_kill(self):
        code = """if 1:
            import signal
            import threading
            import sys

            signum = signal.SIGUSR1

            def handler(signum, frame):
                1/0

            signal.signal(signum, handler)

            tid = threading.get_ident()
            try:
                signal.pthread_kill(tid, signum)
            except ZeroDivisionError:
                pass
            else:
                raise Exception("ZeroDivisionError not raised")
        """
        assert_python_ok('-c', code) 
Example 22
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_signal.py    GNU General Public License v2.0 6 votes vote down vote up
def test_pthread_kill_main_thread(self):
        # Test that a signal can be sent to the main thread with pthread_kill()
        # before any other thread has been created (see issue #12392).
        code = """if True:
            import threading
            import signal
            import sys

            def handler(signum, frame):
                sys.exit(3)

            signal.signal(signal.SIGUSR1, handler)
            signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
            sys.exit(2)
        """

        with spawn_python('-c', code) as process:
            stdout, stderr = process.communicate()
            exitcode = process.wait()
            if exitcode != 3:
                raise Exception("Child error (exit code %s): %s" %
                                (exitcode, stdout)) 
Example 23
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 24
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 25
Project: aws-lambda-runtime-pypy   Author: uscheller   File: base_events.py    Apache License 2.0 6 votes vote down vote up
def _check_thread(self):
        """Check that the current thread is the thread running the event loop.

        Non-thread-safe methods of this class make this assumption and will
        likely behave incorrectly when the assumption is violated.

        Should only be called when (self._debug == True).  The caller is
        responsible for checking this condition for performance reasons.
        """
        if self._thread_id is None:
            return
        thread_id = threading.get_ident()
        if thread_id != self._thread_id:
            raise RuntimeError(
                "Non-thread-safe operation invoked on an event loop other "
                "than the current one") 
Example 26
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def get_ident() -> str:
        """Return some unique identifier for this runner."""
        return str(threading.get_ident()) 
Example 27
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _start_pull(self) -> None:
        def on_payload(plugin: Any, payload: Payload) -> None:  # pylint: disable=unused-argument
            for push in self.task.pushes:
                self.logger.debug(
                    "[Task-%s] Queing item '%s' for push '%s'",
                    self.get_ident(),
                    payload,
                    push
                )
                self.queue.put((payload, push))

        self.task.pull.instance.on_payload = on_payload  # type: ignore

        while not self.stopped.is_set():
            try:
                self.task.pull.instance.pull()
                if not self.stopped.is_set():
                    # Bad thing... Pulling exited unexpectedly
                    self.logger.error(
                        "[Task-%s] Pulling of '%s' exited unexpectedly",
                        self.get_ident(),
                        self.task.pull.instance,
                    )
            except KeyboardInterrupt:  # pragma: no cover
                self.logger.debug(
                    "[Task-%s] Pulling of '%s' hit a keyboard interrupt",
                    self.get_ident(),
                    self.task.pull.instance
                )
            except:  # pylint: disable=bare-except
                # Bad thing... Pulling exited with exception
                self.logger.exception(
                    "[Task-%s] Pulling of '%s' raised an error",
                    self.get_ident(),
                    self.task.pull.instance.name
                )
            finally:
                self._handle_pull_exit() 
Example 28
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _handle_pull_exit(self) -> None:
        if not self.stopped.is_set():
            directive = self.retry_handler.handle_error()
            if directive.abort:
                self.logger.error(
                    "[Task-%s] Pulling of '%s' exited due to retry limitation",
                    self.get_ident(),
                    self.task.pull.instance,
                )
                self.stopped.set()
            else:
                sleep_until_interrupt(directive.wait_for, self.stopped.is_set) 
Example 29
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def stop(self) -> None:
        """Stop the runner gracefully."""
        self.logger.info(
            "[Task-%s] Got stopping signal: '%s'",
            self.get_ident(),
            self.task.name
        )
        self._assert_runner()
        self.stopped.set()
        self.task.pull.instance.stop() 
Example 30
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def get_ident() -> str:
        """Return a unique identifier for this worker."""
        return str(threading.get_ident()) 
Example 31
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _run_queue_worker(self) -> None:
        n_worker = self.queue_worker
        for i in range(n_worker):
            thr = self._make_worker()
            thr.start()
            self.logger.info("[Worker-%s] Started (%s/%s)", thr.get_ident(), i + 1, n_worker)
            self.worker.append(thr) 
Example 32
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _run_tasks(self, tasks: TaskSet) -> None:
        for _, task in tasks.items():
            thr = self._make_runner(task, copy.deepcopy(cast(RetryHandler, self.retry_handler)))
            thr.start()
            self.logger.info("[Task-%s] Started for task '%s'", thr.get_ident(), task.name)
            self.runner.append(thr) 
Example 33
Project: flic-hue   Author: richardtguy   File: fliclib.py    GNU General Public License v3.0 5 votes vote down vote up
def close(self):
		"""Closes the client. The handle_events() method will return."""
		with self._lock:
			if self._closed:
				return
			
			if threading.get_ident() != self._handle_event_thread_ident:
				self._send_command("CmdPing", {"ping_id": 0}) # To unblock socket select
			
			self._closed = True 
Example 34
Project: flic-hue   Author: richardtguy   File: fliclib.py    GNU General Public License v3.0 5 votes vote down vote up
def set_timer(self, timeout_millis, callback):
		"""Set a timer
		
		This timer callback will run after the specified timeout_millis on the thread that handles the events.
		"""
		point_in_time = time.monotonic() + timeout_millis / 1000.0
		self._timers.put((point_in_time, callback))
		
		if threading.get_ident() != self._handle_event_thread_ident:
			self._send_command("CmdPing", {"ping_id": 0}) # To unblock socket select 
Example 35
Project: flic-hue   Author: richardtguy   File: fliclib.py    GNU General Public License v3.0 5 votes vote down vote up
def run_on_handle_events_thread(self, callback):
		"""Run a function on the thread that handles the events."""
		if threading.get_ident() == self._handle_event_thread_ident:
			callback()
		else:
			self.set_timer(0, callback) 
Example 36
Project: flic-hue   Author: richardtguy   File: fliclib.py    GNU General Public License v3.0 5 votes vote down vote up
def handle_events(self):
		"""Start the main loop for this client.
		
		This method will not return until the socket has been closed.
		Once it has returned, any use of this FlicClient is illegal.
		"""
		self._handle_event_thread_ident = threading.get_ident()
		while not self._closed:
			if not self._handle_one_event():
				break
		self._sock.close() 
Example 37
Project: pyrex   Author: garmin   File: test.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.build_dir = os.path.join(PYREX_ROOT, "build", "%d" % os.getpid())

        def cleanup_build():
            if os.path.isdir(self.build_dir):
                shutil.rmtree(self.build_dir)

        self.addCleanup(cleanup_build)
        cleanup_build()
        os.makedirs(self.build_dir)

        helper = os.path.join(PYREX_ROOT, "ci", "%s-helper.py" % self.docker_provider)
        if os.path.exists(helper) and os.environ.get("USE_HELPER", "0") == "1":
            self.dockerpath = helper
        else:
            self.dockerpath = self.docker_provider

        self.pyrex_conf = self.prepare_build_dir(self.build_dir)

        def cleanup_env():
            os.environ.clear()
            os.environ.update(self.old_environ)

        # OE requires that "python" be python2, not python3
        self.bin_dir = os.path.join(self.build_dir, "bin")
        self.old_environ = os.environ.copy()
        os.makedirs(self.bin_dir)
        os.symlink("/usr/bin/python2", os.path.join(self.bin_dir, "python"))
        os.environ["PATH"] = self.bin_dir + ":" + os.environ["PATH"]
        os.environ["PYREX_DOCKER_BUILD_QUIET"] = "0"
        self.addCleanup(cleanup_env)

        self.thread_dir = os.path.join(
            self.build_dir, "%d.%d" % (os.getpid(), threading.get_ident())
        )
        os.makedirs(self.thread_dir) 
Example 38
Project: pyrex   Author: garmin   File: test.py    Apache License 2.0 5 votes vote down vote up
def test_templateconf_abs(self):
        template_dir = os.path.join(self.thread_dir, "template")
        os.makedirs(template_dir)

        self.assertTrue(os.path.isabs(template_dir))

        shutil.copyfile(
            os.path.join(PYREX_ROOT, "poky/meta-poky/conf/local.conf.sample"),
            os.path.join(template_dir, "local.conf.sample"),
        )
        shutil.copyfile(
            os.path.join(PYREX_ROOT, "poky/meta-poky/conf/bblayers.conf.sample"),
            os.path.join(template_dir, "bblayers.conf.sample"),
        )

        test_string = "set_by_test.%d" % threading.get_ident()

        # Write out a config template that passes along the TEST_ENV variable.
        # The variable will only have the correct value in the container if
        # the template is used
        conf = self.get_config()
        conf["run"]["envvars"] += " TEST_ENV"
        with open(os.path.join(template_dir, "pyrex.ini.sample"), "w") as f:
            conf.write(f)
        # Delete the normal pyrex conf file so a new one will be pulled from
        # TEMPLATECONF
        os.unlink(self.pyrex_conf)

        env = os.environ.copy()
        env["TEMPLATECONF"] = template_dir
        env["TEST_ENV"] = test_string

        s = self.assertPyrexContainerShellCommand(
            "echo $TEST_ENV", env=env, quiet_init=True, capture=True
        )
        self.assertEqual(s, test_string) 
Example 39
Project: pyrex   Author: garmin   File: test.py    Apache License 2.0 5 votes vote down vote up
def test_templateconf_rel(self):
        template_dir = os.path.join(self.thread_dir, "template")
        os.makedirs(template_dir)

        self.assertTrue(os.path.isabs(template_dir))

        shutil.copyfile(
            os.path.join(PYREX_ROOT, "poky/meta-poky/conf/local.conf.sample"),
            os.path.join(template_dir, "local.conf.sample"),
        )
        shutil.copyfile(
            os.path.join(PYREX_ROOT, "poky/meta-poky/conf/bblayers.conf.sample"),
            os.path.join(template_dir, "bblayers.conf.sample"),
        )

        test_string = "set_by_test.%d" % threading.get_ident()

        # Write out a config template that passes along the TEST_ENV variable.
        # The variable will only have the correct value in the container if
        # the template is used
        conf = self.get_config()
        conf["run"]["envvars"] += " TEST_ENV"
        with open(os.path.join(template_dir, "pyrex.ini.sample"), "w") as f:
            conf.write(f)
        # Delete the normal pyrex conf file so a new one will be pulled from
        # TEMPLATECONF
        os.unlink(self.pyrex_conf)

        env = os.environ.copy()
        env["TEMPLATECONF"] = os.path.relpath(
            template_dir, os.path.join(PYREX_ROOT, "poky")
        )
        env["TEST_ENV"] = test_string

        s = self.assertPyrexContainerShellCommand(
            "echo $TEST_ENV", env=env, quiet_init=True, capture=True
        )
        self.assertEqual(s, test_string) 
Example 40
Project: audit-tool   Author: gwu-libraries   File: audit_tool.py    MIT License 5 votes vote down vote up
def _get_conn(self):
        thread_id = get_ident()
        if thread_id not in self._connection_cache:
            self._connection_cache[thread_id] = sqlite3.connect(self.db_filepath,
                                                                detect_types=(sqlite3.PARSE_DECLTYPES
                                                                              | sqlite3.PARSE_COLNAMES))

        return self._connection_cache[thread_id] 
Example 41
Project: leaguedirector   Author: RiotGames   File: bindings.py    Apache License 2.0 5 votes vote down vote up
def run(self):
        self.tid = threading.get_ident()
        if platform.system() == 'Windows':
            self.run_windows()
        elif platform.system() == 'Darwin':
            self.run_mac() 
Example 42
Project: NiujiaoDebugger   Author: MrSrc   File: base_events.py    GNU General Public License v3.0 5 votes vote down vote up
def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')
        self._set_coroutine_origin_tracking(self._debug)
        self._thread_id = threading.get_ident()

        old_agen_hooks = sys.get_asyncgen_hooks()
        sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                               finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = None
            events._set_running_loop(None)
            self._set_coroutine_origin_tracking(False)
            sys.set_asyncgen_hooks(*old_agen_hooks) 
Example 43
Project: NiujiaoDebugger   Author: MrSrc   File: lock_tests.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, f, n, wait_before_exit=False):
        """
        Construct a bunch of `n` threads running the same function `f`.
        If `wait_before_exit` is True, the threads won't terminate until
        do_finish() is called.
        """
        self.f = f
        self.n = n
        self.started = []
        self.finished = []
        self._can_exit = not wait_before_exit
        self.wait_thread = support.wait_threads_exit()
        self.wait_thread.__enter__()

        def task():
            tid = threading.get_ident()
            self.started.append(tid)
            try:
                f()
            finally:
                self.finished.append(tid)
                while not self._can_exit:
                    _wait()

        try:
            for i in range(n):
                start_new_thread(task, ())
        except:
            self._can_exit = True
            raise 
Example 44
Project: NiujiaoDebugger   Author: MrSrc   File: test_time.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pthread_getcpuclockid(self):
        clk_id = time.pthread_getcpuclockid(threading.get_ident())
        self.assertTrue(type(clk_id) is int)
        self.assertNotEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID)
        t1 = time.clock_gettime(clk_id)
        t2 = time.clock_gettime(clk_id)
        self.assertLessEqual(t1, t2) 
Example 45
Project: NiujiaoDebugger   Author: MrSrc   File: test_futures.py    GNU General Public License v3.0 5 votes vote down vote up
def test_wrap_future_use_global_loop(self):
        with mock.patch('asyncio.futures.events') as events:
            events.get_event_loop = lambda: self.loop
            def run(arg):
                return (arg, threading.get_ident())
            ex = concurrent.futures.ThreadPoolExecutor(1)
            f1 = ex.submit(run, 'oi')
            f2 = asyncio.wrap_future(f1)
            self.assertIs(self.loop, f2._loop)
            ex.shutdown(wait=True) 
Example 46
Project: NiujiaoDebugger   Author: MrSrc   File: test_events.py    GNU General Public License v3.0 5 votes vote down vote up
def test_run_in_executor(self):
        def run(arg):
            return (arg, threading.get_ident())
        f2 = self.loop.run_in_executor(None, run, 'yo')
        res, thread_id = self.loop.run_until_complete(f2)
        self.assertEqual(res, 'yo')
        self.assertNotEqual(thread_id, threading.get_ident()) 
Example 47
Project: Pyro5   Author: irmen   File: server2.py    MIT License 5 votes vote down vote up
def increment_oneway(self):
        print("oneway call executing in thread", threading.get_ident())
        time.sleep(0.5)
        self.counter += 1 
Example 48
Project: Pyro5   Author: irmen   File: client.py    MIT License 5 votes vote down vote up
def __init__(self, uri, connected_socket=None):
        if connected_socket:
            uri = core.URI("PYRO:" + uri + "@<<connected-socket>>:0")
        if isinstance(uri, str):
            uri = core.URI(uri)
        elif not isinstance(uri, core.URI):
            raise TypeError("expected Pyro URI")
        self._pyroUri = uri
        self._pyroConnection = None
        self._pyroSerializer = None  # can be set to the name of a serializer to override the global one per-proxy
        self._pyroMethods = set()  # all methods of the remote object, gotten from meta-data
        self._pyroAttrs = set()  # attributes of the remote object, gotten from meta-data
        self._pyroOneway = set()  # oneway-methods of the remote object, gotten from meta-data
        self._pyroSeq = 0  # message sequence number
        self._pyroRawWireResponse = False  # internal switch to enable wire level responses
        self._pyroHandshake = "hello"  # the data object that should be sent in the initial connection handshake message
        self._pyroMaxRetries = config.MAX_RETRIES
        self.__pyroTimeout = config.COMMTIMEOUT
        self.__pyroOwnerThread = get_ident()     # the thread that owns this proxy
        if config.SERIALIZER not in serializers.serializers:
            raise ValueError("unknown serializer configured")
        # note: we're not clearing the client annotations dict here.
        #       that is because otherwise it will be wiped if a new proxy is needed to connect PYRONAME uris.
        #       clearing the response annotations is okay.
        core.current_context.response_annotations = {}
        if connected_socket:
            self.__pyroCreateConnection(False, connected_socket) 
Example 49
Project: Pyro5   Author: irmen   File: client.py    MIT License 5 votes vote down vote up
def __setstate__(self, state):
        self._pyroUri = core.URI(state[0])
        self._pyroOneway = set(state[1])
        self._pyroMethods = set(state[2])
        self._pyroAttrs = set(state[3])
        self._pyroHandshake = state[4]
        self._pyroSerializer = state[5]
        self.__pyroTimeout = config.COMMTIMEOUT
        self._pyroMaxRetries = config.MAX_RETRIES
        self._pyroConnection = None
        self._pyroSeq = 0
        self._pyroRawWireResponse = False
        self.__pyroOwnerThread = get_ident() 
Example 50
Project: Pyro5   Author: irmen   File: client.py    MIT License 5 votes vote down vote up
def _pyroClaimOwnership(self):
        """
        The current thread claims the ownership of this proxy from another thread.
        Any existing connection will remain active!
        """
        if get_ident() != self.__pyroOwnerThread:
            # if self._pyroConnection is not None:
            #     self._pyroConnection.close()
            #     self._pyroConnection = None
            self.__pyroOwnerThread = get_ident() 
Example 51
Project: Pyro5   Author: irmen   File: client.py    MIT License 5 votes vote down vote up
def __check_owner(self):
        if get_ident() != self.__pyroOwnerThread:
            raise errors.PyroError("the calling thread is not the owner of this proxy, "
                                   "create a new proxy in this thread or transfer ownership.") 
Example 52
Project: labchain   Author: ChaAC-FAU   File: chainbuilder.py    GNU General Public License v3.0 5 votes vote down vote up
def _assert_thread_safety(self):
        if self._thread_id is None:
            self._thread_id = threading.get_ident()
        assert self._thread_id == threading.get_ident() 
Example 53
Project: picobox   Author: ikalnytskyi   File: example.py    MIT License 5 votes vote down vote up
def spam(session):
    return 'thread={}; session={}; ip={}'.format(
        threading.get_ident(),
        id(session),
        session.get('https://httpbin.org/ip').json()['origin'])


# According to https://github.com/kennethreitz/requests/issues/2766
# requests.Session() is not thread-safe. Therefore we need to create
# a separate session for each thread. 
Example 54
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def watch(event, target, config):
    message = list(config.keys())[0]
    actions = config[message]

    flags = re.I if args.enable_regex_ignorecase else 0
    regex = re.compile(r"{0}".format(message), flags=flags)

    log.logger.info("watch {0}".format(target))
    tail    = Tail(event, interval=args.interval)

    log.logger.debug("thread_id {0}: start thread.".format(threading.get_ident()))
    log.logger.debug("target:{0} message:{1}".format(target, message))
    for line in tail.tail_f(target):
        result = regex.search(line)
        if not result:
            continue
        log.logger.info("match line in {0}: {1}".format(target, line))
        if not args.disable_shell_escape:
            line = shlex.quote(line)
        for action in actions:
            action = action.replace(REPLACE_STRING, line)
            if len(result.groups()) > 0:
                i = 1
                for group in result.groups():
                    if not args.disable_shell_escape:
                        group = shlex.quote(group)
                    replace_string = REPLACE_STRING_EX.replace("N", str(i))
                    action = action.replace(replace_string, group)
                    i = i + 1

            log.logger.info("action: {0}".format(action))
            subprocess.call(action, shell=True, timeout=ACTION_TIMEOUT)

    log.logger.debug("thread_id {0}: break thread.".format(threading.get_ident())) 
Example 55
Project: ACE   Author: IntegralDefense   File: views.py    Apache License 2.0 5 votes vote down vote up
def debug():

    import threading
    import os

    message = """

current thread id = {}
current pid = {}
thread count = {}

""".format(threading.get_ident(), os.getpid(), threading.active_count())
    
    return message, 200 
Example 56
Project: ACE   Author: IntegralDefense   File: database.py    Apache License 2.0 5 votes vote down vote up
def _get_cache_identifier():
    """Returns the key for _use_cache_flags"""
    return '{}:{}'.format(os.getpid(), threading.get_ident()) 
Example 57
Project: ACE   Author: IntegralDefense   File: database.py    Apache License 2.0 5 votes vote down vote up
def _get_cached_db_identifier(name):
    """Returns the key for _global_db_cache"""
    return '{}:{}:{}'.format(str(os.getpid()), str(threading.get_ident()), name) 
Example 58
Project: unhuman-resources   Author: agajdosi   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def initialize(  # type: ignore
        self, asyncio_loop: asyncio.AbstractEventLoop, **kwargs: Any
    ) -> None:
        self.asyncio_loop = asyncio_loop
        # Maps fd to (fileobj, handler function) pair (as in IOLoop.add_handler)
        self.handlers = {}  # type: Dict[int, Tuple[Union[int, _Selectable], Callable]]
        # Set of fds listening for reads/writes
        self.readers = set()  # type: Set[int]
        self.writers = set()  # type: Set[int]
        self.closing = False
        # If an asyncio loop was closed through an asyncio interface
        # instead of IOLoop.close(), we'd never hear about it and may
        # have left a dangling reference in our map. In case an
        # application (or, more likely, a test suite) creates and
        # destroys a lot of event loops in this way, check here to
        # ensure that we don't have a lot of dead loops building up in
        # the map.
        #
        # TODO(bdarnell): consider making self.asyncio_loop a weakref
        # for AsyncIOMainLoop and make _ioloop_for_asyncio a
        # WeakKeyDictionary.
        for loop in list(IOLoop._ioloop_for_asyncio):
            if loop.is_closed():
                del IOLoop._ioloop_for_asyncio[loop]
        IOLoop._ioloop_for_asyncio[asyncio_loop] = self

        self._thread_identity = 0

        super(BaseAsyncIOLoop, self).initialize(**kwargs)

        def assign_thread_identity() -> None:
            self._thread_identity = get_ident()

        self.add_callback(assign_thread_identity) 
Example 59
Project: unhuman-resources   Author: agajdosi   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def add_callback(self, callback: Callable, *args: Any, **kwargs: Any) -> None:
        if get_ident() == self._thread_identity:
            call_soon = self.asyncio_loop.call_soon
        else:
            call_soon = self.asyncio_loop.call_soon_threadsafe
        try:
            call_soon(self._run_callback, functools.partial(callback, *args, **kwargs))
        except RuntimeError:
            # "Event loop is closed". Swallow the exception for
            # consistency with PollIOLoop (and logical consistency
            # with the fact that we can't guarantee that an
            # add_callback that completes without error will
            # eventually execute).
            pass 
Example 60
Project: unhuman-resources   Author: agajdosi   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def initialize(  # type: ignore
        self, asyncio_loop: asyncio.AbstractEventLoop, **kwargs: Any
    ) -> None:
        self.asyncio_loop = asyncio_loop
        # Maps fd to (fileobj, handler function) pair (as in IOLoop.add_handler)
        self.handlers = {}  # type: Dict[int, Tuple[Union[int, _Selectable], Callable]]
        # Set of fds listening for reads/writes
        self.readers = set()  # type: Set[int]
        self.writers = set()  # type: Set[int]
        self.closing = False
        # If an asyncio loop was closed through an asyncio interface
        # instead of IOLoop.close(), we'd never hear about it and may
        # have left a dangling reference in our map. In case an
        # application (or, more likely, a test suite) creates and
        # destroys a lot of event loops in this way, check here to
        # ensure that we don't have a lot of dead loops building up in
        # the map.
        #
        # TODO(bdarnell): consider making self.asyncio_loop a weakref
        # for AsyncIOMainLoop and make _ioloop_for_asyncio a
        # WeakKeyDictionary.
        for loop in list(IOLoop._ioloop_for_asyncio):
            if loop.is_closed():
                del IOLoop._ioloop_for_asyncio[loop]
        IOLoop._ioloop_for_asyncio[asyncio_loop] = self

        self._thread_identity = 0

        super(BaseAsyncIOLoop, self).initialize(**kwargs)

        def assign_thread_identity() -> None:
            self._thread_identity = get_ident()

        self.add_callback(assign_thread_identity) 
Example 61
Project: unhuman-resources   Author: agajdosi   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def add_callback(self, callback: Callable, *args: Any, **kwargs: Any) -> None:
        if get_ident() == self._thread_identity:
            call_soon = self.asyncio_loop.call_soon
        else:
            call_soon = self.asyncio_loop.call_soon_threadsafe
        try:
            call_soon(self._run_callback, functools.partial(callback, *args, **kwargs))
        except RuntimeError:
            # "Event loop is closed". Swallow the exception for
            # consistency with PollIOLoop (and logical consistency
            # with the fact that we can't guarantee that an
            # add_callback that completes without error will
            # eventually execute).
            pass 
Example 62
Project: opendevops   Author: opendevops-cn   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def initialize(  # type: ignore
        self, asyncio_loop: asyncio.AbstractEventLoop, **kwargs: Any
    ) -> None:
        self.asyncio_loop = asyncio_loop
        # Maps fd to (fileobj, handler function) pair (as in IOLoop.add_handler)
        self.handlers = {}  # type: Dict[int, Tuple[Union[int, _Selectable], Callable]]
        # Set of fds listening for reads/writes
        self.readers = set()  # type: Set[int]
        self.writers = set()  # type: Set[int]
        self.closing = False
        # If an asyncio loop was closed through an asyncio interface
        # instead of IOLoop.close(), we'd never hear about it and may
        # have left a dangling reference in our map. In case an
        # application (or, more likely, a test suite) creates and
        # destroys a lot of event loops in this way, check here to
        # ensure that we don't have a lot of dead loops building up in
        # the map.
        #
        # TODO(bdarnell): consider making self.asyncio_loop a weakref
        # for AsyncIOMainLoop and make _ioloop_for_asyncio a
        # WeakKeyDictionary.
        for loop in list(IOLoop._ioloop_for_asyncio):
            if loop.is_closed():
                del IOLoop._ioloop_for_asyncio[loop]
        IOLoop._ioloop_for_asyncio[asyncio_loop] = self

        self._thread_identity = 0

        super(BaseAsyncIOLoop, self).initialize(**kwargs)

        def assign_thread_identity() -> None:
            self._thread_identity = get_ident()

        self.add_callback(assign_thread_identity) 
Example 63
Project: opendevops   Author: opendevops-cn   File: asyncio.py    GNU General Public License v3.0 5 votes vote down vote up
def add_callback(self, callback: Callable, *args: Any, **kwargs: Any) -> None:
        if get_ident() == self._thread_identity:
            call_soon = self.asyncio_loop.call_soon
        else:
            call_soon = self.asyncio_loop.call_soon_threadsafe
        try:
            call_soon(self._run_callback, functools.partial(callback, *args, **kwargs))
        except RuntimeError:
            # "Event loop is closed". Swallow the exception for
            # consistency with PollIOLoop (and logical consistency
            # with the fact that we can't guarantee that an
            # add_callback that completes without error will
            # eventually execute).
            pass 
Example 64
Project: datadog-unix-agent   Author: DataDog   File: test_signals.py    Apache License 2.0 5 votes vote down vote up
def test_signal_delivery(signal_handler):
    dummy = DummyComponent()
    signal_handler.register('dummy', dummy)

    signal_handler.start()
    sleep(1)  # just enough time to get started

    signal.pthread_kill(threading.get_ident(), signal.SIGUSR1)
    sleep(1)  # signal delivery lapse

    assert(dummy._stopped) 
Example 65
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_signal.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pthread_kill(self):
        code = """if 1:
            import signal
            import threading
            import sys

            signum = signal.SIGUSR1

            def handler(signum, frame):
                1/0

            signal.signal(signum, handler)

            if sys.platform == 'freebsd6':
                # Issue #12392 and #12469: send a signal to the main thread
                # doesn't work before the creation of the first thread on
                # FreeBSD 6
                def noop():
                    pass
                thread = threading.Thread(target=noop)
                thread.start()
                thread.join()

            tid = threading.get_ident()
            try:
                signal.pthread_kill(tid, signum)
            except ZeroDivisionError:
                pass
            else:
                raise Exception("ZeroDivisionError not raised")
        """
        assert_python_ok('-c', code) 
Example 66
Project: Blockly-rduino-communication   Author: technologiescollege   File: lock_tests.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, f, n, wait_before_exit=False):
        """
        Construct a bunch of `n` threads running the same function `f`.
        If `wait_before_exit` is True, the threads won't terminate until
        do_finish() is called.
        """
        self.f = f
        self.n = n
        self.started = []
        self.finished = []
        self._can_exit = not wait_before_exit
        def task():
            tid = threading.get_ident()
            self.started.append(tid)
            try:
                f()
            finally:
                self.finished.append(tid)
                while not self._can_exit:
                    _wait()
        try:
            for i in range(n):
                start_new_thread(task, ())
        except:
            self._can_exit = True
            raise 
Example 67
Project: poodle   Author: criticalhop   File: poodle_util.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _p_get_meta():
    # TODO: clean-up when thread finishes
    return _p_metadata[threading.get_ident()] 
Example 68
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: base_events.py    GNU General Public License v2.0 5 votes vote down vote up
def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')
        self._set_coroutine_origin_tracking(self._debug)
        self._thread_id = threading.get_ident()

        old_agen_hooks = sys.get_asyncgen_hooks()
        sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                               finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = None
            events._set_running_loop(None)
            self._set_coroutine_origin_tracking(False)
            sys.set_asyncgen_hooks(*old_agen_hooks) 
Example 69
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: lock_tests.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, f, n, wait_before_exit=False):
        """
        Construct a bunch of `n` threads running the same function `f`.
        If `wait_before_exit` is True, the threads won't terminate until
        do_finish() is called.
        """
        self.f = f
        self.n = n
        self.started = []
        self.finished = []
        self._can_exit = not wait_before_exit
        self.wait_thread = support.wait_threads_exit()
        self.wait_thread.__enter__()

        def task():
            tid = threading.get_ident()
            self.started.append(tid)
            try:
                f()
            finally:
                self.finished.append(tid)
                while not self._can_exit:
                    _wait()

        try:
            for i in range(n):
                start_new_thread(task, ())
        except:
            self._can_exit = True
            raise 
Example 70
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_time.py    GNU General Public License v2.0 5 votes vote down vote up
def test_pthread_getcpuclockid(self):
        clk_id = time.pthread_getcpuclockid(threading.get_ident())
        self.assertTrue(type(clk_id) is int)
        self.assertNotEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID)
        t1 = time.clock_gettime(clk_id)
        t2 = time.clock_gettime(clk_id)
        self.assertLessEqual(t1, t2) 
Example 71
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_futures.py    GNU General Public License v2.0 5 votes vote down vote up
def test_wrap_future_use_global_loop(self):
        with mock.patch('asyncio.futures.events') as events:
            events.get_event_loop = lambda: self.loop
            def run(arg):
                return (arg, threading.get_ident())
            ex = concurrent.futures.ThreadPoolExecutor(1)
            f1 = ex.submit(run, 'oi')
            f2 = asyncio.wrap_future(f1)
            self.assertIs(self.loop, f2._loop)
            ex.shutdown(wait=True) 
Example 72
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_events.py    GNU General Public License v2.0 5 votes vote down vote up
def test_run_in_executor(self):
        def run(arg):
            return (arg, threading.get_ident())
        f2 = self.loop.run_in_executor(None, run, 'yo')
        res, thread_id = self.loop.run_until_complete(f2)
        self.assertEqual(res, 'yo')
        self.assertNotEqual(thread_id, threading.get_ident()) 
Example 73
Project: aws-lambda-runtime-pypy   Author: uscheller   File: base_events.py    Apache License 2.0 5 votes vote down vote up
def run_forever(self):
        """Run until stop() is called."""
        self._check_closed()
        if self.is_running():
            raise RuntimeError('This event loop is already running')
        if events._get_running_loop() is not None:
            raise RuntimeError(
                'Cannot run the event loop while another loop is running')
        self._set_coroutine_wrapper(self._debug)
        self._thread_id = threading.get_ident()
        if self._asyncgens is not None:
            old_agen_hooks = sys.get_asyncgen_hooks()
            sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
                                   finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = None
            events._set_running_loop(None)
            self._set_coroutine_wrapper(False)
            if self._asyncgens is not None:
                sys.set_asyncgen_hooks(*old_agen_hooks) 
Example 74
Project: vscode-mayapy   Author: FXTD-ODYSSEY   File: attach_script.py    MIT License 4 votes vote down vote up
def fix_main_thread_id(on_warn=lambda msg:None, on_exception=lambda msg:None, on_critical=lambda msg:None):
    # This means that we weren't able to import threading in the main thread (which most
    # likely means that the main thread is paused or in some very long operation).
    # In this case we'll import threading here and hotfix what may be wrong in the threading
    # module (if we're on Windows where we create a thread to do the attach and on Linux
    # we are not certain on which thread we're executing this code).
    #
    # The code below is a workaround for https://bugs.python.org/issue37416
    import sys
    import threading

    try:
        with threading._active_limbo_lock:
            main_thread_instance = get_main_thread_instance(threading)

            if sys.platform == 'win32':
                # On windows this code would be called in a secondary thread, so,
                # the current thread is unlikely to be the main thread.
                if hasattr(threading, '_get_ident'):
                    unlikely_thread_id = threading._get_ident()  # py2
                else:
                    unlikely_thread_id = threading.get_ident()  # py3
            else:
                unlikely_thread_id = None

            main_thread_id, critical_warning = get_main_thread_id(unlikely_thread_id)

            if main_thread_id is not None:
                main_thread_id_attr = '_ident'
                if not hasattr(main_thread_instance, main_thread_id_attr):
                    main_thread_id_attr = '_Thread__ident'
                    assert hasattr(main_thread_instance, main_thread_id_attr)

                if main_thread_id != getattr(main_thread_instance, main_thread_id_attr):
                    # Note that we also have to reset the '_tstack_lock' for a regular lock.
                    # This is needed to avoid an error on shutdown because this lock is bound
                    # to the thread state and will be released when the secondary thread
                    # that initialized the lock is finished -- making an assert fail during
                    # process shutdown.
                    main_thread_instance._tstate_lock = threading._allocate_lock()
                    main_thread_instance._tstate_lock.acquire()

                    # Actually patch the thread ident as well as the threading._active dict
                    # (we should have the _active_limbo_lock to do that).
                    threading._active.pop(getattr(main_thread_instance, main_thread_id_attr), None)
                    setattr(main_thread_instance, main_thread_id_attr, main_thread_id)
                    threading._active[getattr(main_thread_instance, main_thread_id_attr)] = main_thread_instance

        # Note: only import from pydevd after the patching is done (we want to do the minimum
        # possible when doing that patching).
        on_warn('The threading module was not imported by user code in the main thread. The debugger will attempt to work around https://bugs.python.org/issue37416.')

        if critical_warning:
            on_critical('Issue found when debugger was trying to work around https://bugs.python.org/issue37416:\n%s' % (critical_warning,))
    except:
        on_exception('Error patching main thread id.') 
Example 75
Project: NiujiaoDebugger   Author: MrSrc   File: test_socketserver.py    GNU General Public License v3.0 4 votes vote down vote up
def test_write(self):
        # Test that wfile.write() sends data immediately, and that it does
        # not truncate sends when interrupted by a Unix signal
        pthread_kill = test.support.get_attribute(signal, 'pthread_kill')

        class Handler(socketserver.StreamRequestHandler):
            def handle(self):
                self.server.sent1 = self.wfile.write(b'write data\n')
                # Should be sent immediately, without requiring flush()
                self.server.received = self.rfile.readline()
                big_chunk = b'\0' * test.support.SOCK_MAX_SIZE
                self.server.sent2 = self.wfile.write(big_chunk)

        server = socketserver.TCPServer((HOST, 0), Handler)
        self.addCleanup(server.server_close)
        interrupted = threading.Event()

        def signal_handler(signum, frame):
            interrupted.set()

        original = signal.signal(signal.SIGUSR1, signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original)
        response1 = None
        received2 = None
        main_thread = threading.get_ident()

        def run_client():
            s = socket.socket(server.address_family, socket.SOCK_STREAM,
                socket.IPPROTO_TCP)
            with s, s.makefile('rb') as reader:
                s.connect(server.server_address)
                nonlocal response1
                response1 = reader.readline()
                s.sendall(b'client response\n')

                reader.read(100)
                # The main thread should now be blocking in a send() syscall.
                # But in theory, it could get interrupted by other signals,
                # and then retried. So keep sending the signal in a loop, in
                # case an earlier signal happens to be delivered at an
                # inconvenient moment.
                while True:
                    pthread_kill(main_thread, signal.SIGUSR1)
                    if interrupted.wait(timeout=float(1)):
                        break
                nonlocal received2
                received2 = len(reader.read())

        background = threading.Thread(target=run_client)
        background.start()
        server.handle_request()
        background.join()
        self.assertEqual(server.sent1, len(response1))
        self.assertEqual(response1, b'write data\n')
        self.assertEqual(server.received, b'client response\n')
        self.assertEqual(server.sent2, test.support.SOCK_MAX_SIZE)
        self.assertEqual(received2, test.support.SOCK_MAX_SIZE - 100) 
Example 76
Project: NiujiaoDebugger   Author: MrSrc   File: test_wsgiref.py    GNU General Public License v3.0 4 votes vote down vote up
def test_interrupted_write(self):
        # BaseHandler._write() and _flush() have to write all data, even if
        # it takes multiple send() calls.  Test this by interrupting a send()
        # call with a Unix signal.
        pthread_kill = support.get_attribute(signal, "pthread_kill")

        def app(environ, start_response):
            start_response("200 OK", [])
            return [b'\0' * support.SOCK_MAX_SIZE]

        class WsgiHandler(NoLogRequestHandler, WSGIRequestHandler):
            pass

        server = make_server(support.HOST, 0, app, handler_class=WsgiHandler)
        self.addCleanup(server.server_close)
        interrupted = threading.Event()

        def signal_handler(signum, frame):
            interrupted.set()

        original = signal.signal(signal.SIGUSR1, signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original)
        received = None
        main_thread = threading.get_ident()

        def run_client():
            http = HTTPConnection(*server.server_address)
            http.request("GET", "/")
            with http.getresponse() as response:
                response.read(100)
                # The main thread should now be blocking in a send() system
                # call.  But in theory, it could get interrupted by other
                # signals, and then retried.  So keep sending the signal in a
                # loop, in case an earlier signal happens to be delivered at
                # an inconvenient moment.
                while True:
                    pthread_kill(main_thread, signal.SIGUSR1)
                    if interrupted.wait(timeout=float(1)):
                        break
                nonlocal received
                received = len(response.read())
            http.close()

        background = threading.Thread(target=run_client)
        background.start()
        server.handle_request()
        background.join()
        self.assertEqual(received, support.SOCK_MAX_SIZE - 100) 
Example 77
Project: nest_asyncio   Author: erdewit   File: nest_asyncio.py    BSD 2-Clause "Simplified" License 4 votes vote down vote up
def _patch_loop(loop):
    """
    Patch loop to make it reentrent.
    """
    if sys.version_info >= (3, 7, 0):
        set_coro_tracking = loop._set_coroutine_origin_tracking
    else:
        set_coro_tracking = loop._set_coroutine_wrapper

    def run_forever(self):
        self._check_closed()
        old_thread_id = self._thread_id
        old_running_loop = events._get_running_loop()
        set_coro_tracking(self._debug)
        self._thread_id = threading.get_ident()

        if self._asyncgens is not None:
            old_agen_hooks = sys.get_asyncgen_hooks()
            sys.set_asyncgen_hooks(
                firstiter=self._asyncgen_firstiter_hook,
                finalizer=self._asyncgen_finalizer_hook)
        try:
            events._set_running_loop(self)
            while True:
                try:
                    self._run_once()
                except IndexError:
                    # Ignore 'pop from an empty deque' errors.
                    # This happens when all ready handles have already been
                    # processed but _run_once expects there to be more.
                    # Since the handles have been processed anyway it is
                    # safe to ignore this.
                    pass
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = old_thread_id
            events._set_running_loop(old_running_loop)
            set_coro_tracking(False)
            if self._asyncgens is not None:
                sys.set_asyncgen_hooks(*old_agen_hooks)

    cls = loop.__class__
    cls._run_forever_orig = cls.run_forever
    cls.run_forever = run_forever 
Example 78
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_socketserver.py    GNU General Public License v2.0 4 votes vote down vote up
def test_write(self):
        # Test that wfile.write() sends data immediately, and that it does
        # not truncate sends when interrupted by a Unix signal
        pthread_kill = test.support.get_attribute(signal, 'pthread_kill')

        class Handler(socketserver.StreamRequestHandler):
            def handle(self):
                self.server.sent1 = self.wfile.write(b'write data\n')
                # Should be sent immediately, without requiring flush()
                self.server.received = self.rfile.readline()
                big_chunk = b'\0' * test.support.SOCK_MAX_SIZE
                self.server.sent2 = self.wfile.write(big_chunk)

        server = socketserver.TCPServer((HOST, 0), Handler)
        self.addCleanup(server.server_close)
        interrupted = threading.Event()

        def signal_handler(signum, frame):
            interrupted.set()

        original = signal.signal(signal.SIGUSR1, signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original)
        response1 = None
        received2 = None
        main_thread = threading.get_ident()

        def run_client():
            s = socket.socket(server.address_family, socket.SOCK_STREAM,
                socket.IPPROTO_TCP)
            with s, s.makefile('rb') as reader:
                s.connect(server.server_address)
                nonlocal response1
                response1 = reader.readline()
                s.sendall(b'client response\n')

                reader.read(100)
                # The main thread should now be blocking in a send() syscall.
                # But in theory, it could get interrupted by other signals,
                # and then retried. So keep sending the signal in a loop, in
                # case an earlier signal happens to be delivered at an
                # inconvenient moment.
                while True:
                    pthread_kill(main_thread, signal.SIGUSR1)
                    if interrupted.wait(timeout=float(1)):
                        break
                nonlocal received2
                received2 = len(reader.read())

        background = threading.Thread(target=run_client)
        background.start()
        server.handle_request()
        background.join()
        self.assertEqual(server.sent1, len(response1))
        self.assertEqual(response1, b'write data\n')
        self.assertEqual(server.received, b'client response\n')
        self.assertEqual(server.sent2, test.support.SOCK_MAX_SIZE)
        self.assertEqual(received2, test.support.SOCK_MAX_SIZE - 100) 
Example 79
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_wsgiref.py    GNU General Public License v2.0 4 votes vote down vote up
def test_interrupted_write(self):
        # BaseHandler._write() and _flush() have to write all data, even if
        # it takes multiple send() calls.  Test this by interrupting a send()
        # call with a Unix signal.
        pthread_kill = support.get_attribute(signal, "pthread_kill")

        def app(environ, start_response):
            start_response("200 OK", [])
            return [b'\0' * support.SOCK_MAX_SIZE]

        class WsgiHandler(NoLogRequestHandler, WSGIRequestHandler):
            pass

        server = make_server(support.HOST, 0, app, handler_class=WsgiHandler)
        self.addCleanup(server.server_close)
        interrupted = threading.Event()

        def signal_handler(signum, frame):
            interrupted.set()

        original = signal.signal(signal.SIGUSR1, signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original)
        received = None
        main_thread = threading.get_ident()

        def run_client():
            http = HTTPConnection(*server.server_address)
            http.request("GET", "/")
            with http.getresponse() as response:
                response.read(100)
                # The main thread should now be blocking in a send() system
                # call.  But in theory, it could get interrupted by other
                # signals, and then retried.  So keep sending the signal in a
                # loop, in case an earlier signal happens to be delivered at
                # an inconvenient moment.
                while True:
                    pthread_kill(main_thread, signal.SIGUSR1)
                    if interrupted.wait(timeout=float(1)):
                        break
                nonlocal received
                received = len(response.read())
            http.close()

        background = threading.Thread(target=run_client)
        background.start()
        server.handle_request()
        background.join()
        self.assertEqual(received, support.SOCK_MAX_SIZE - 100) 
Example 80
Project: aws-lambda-runtime-pypy   Author: uscheller   File: _sqlite3.py    Apache License 2.0 4 votes vote down vote up
def __init__(self, database, timeout=5.0, detect_types=0, isolation_level="",
                 check_same_thread=True, factory=None, cached_statements=100, uri=0):
        self.__initialized = True
        db_star = _ffi.new('sqlite3 **')

        if isinstance(database, unicode):
            database = database.encode('utf-8')
        if _lib.SQLITE_OPEN_URI != 0:
            if uri and _lib.SQLITE_OPEN_URI == 0:
                raise NotSupportedError("URIs not supported")
            flags = _lib.SQLITE_OPEN_READWRITE | _lib.SQLITE_OPEN_CREATE
            if uri:
                flags |= _lib.SQLITE_OPEN_URI
            if _lib.sqlite3_open_v2(database, db_star, flags, _ffi.NULL) != _lib.SQLITE_OK:
                raise OperationalError("Could not open database")
        else:
            if _lib.sqlite3_open(database, db_star) != _lib.SQLITE_OK:
                raise OperationalError("Could not open database")
        self._db = db_star[0]
        if timeout is not None:
            timeout = int(timeout * 1000)  # pysqlite2 uses timeout in seconds
            _lib.sqlite3_busy_timeout(self._db, timeout)

        self.row_factory = None
        self.text_factory = _unicode_text_factory

        self._detect_types = detect_types
        self._in_transaction = False
        self.isolation_level = isolation_level

        self.__cursors = []
        self.__cursors_counter = 0
        self.__statements = []
        self.__statements_counter = 0
        self.__rawstatements = set()
        self._statement_cache = _StatementCache(self, cached_statements)
        self.__statements_already_committed = []

        self.__func_cache = {}
        self.__aggregates = {}
        self.__aggregate_instances = {}
        self.__collations = {}
        if check_same_thread:
            self.__thread_ident = threading.get_ident()
        if not check_same_thread and _lib.sqlite3_libversion_number() < 3003001:
            raise NotSupportedError("shared connections not available")

        self.Error = Error
        self.Warning = Warning
        self.InterfaceError = InterfaceError
        self.DatabaseError = DatabaseError
        self.InternalError = InternalError
        self.OperationalError = OperationalError
        self.ProgrammingError = ProgrammingError
        self.IntegrityError = IntegrityError
        self.DataError = DataError
        self.NotSupportedError = NotSupportedError