Python threading.get_ident() Examples

The following are 30 code examples for showing how to use threading.get_ident(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module threading , or try the search function .

Example 1
Project: discord.py   Author: Rapptz   File: gateway.py    License: MIT License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.max_size = None
        # an empty dispatcher to prevent crashes
        self._dispatch = lambda *args: None
        # generic event listeners
        self._dispatch_listeners = []
        # the keep alive
        self._keep_alive = None
        self.thread_id = threading.get_ident()

        # ws related stuff
        self.session_id = None
        self.sequence = None
        self._zlib = zlib.decompressobj()
        self._buffer = bytearray() 
Example 2
Project: ReadableWebProxy   Author: fake-name   File: misc_db.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_from_db_key_value_store(key):
	global KV_META_CACHE
	kv_log.info("Getting '%s' from kv store", key)
	if key in KV_META_CACHE:
		return KV_META_CACHE[key]

	thread_id = "kv_store_{}".format(threading.get_ident())
	with session_context(thread_id) as sess:
		have = sess.query(KeyValueStore).filter(KeyValueStore.key == key).scalar()
		if have:
			kv_log.info("KV store had entry")
			ret = have.value
		else:
			kv_log.info("KV store did not have entry")
			ret = {}

		sess.commit()

	return ret 
Example 3
Project: panoptes   Author: yahoo   File: helpers.py    License: 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 4
Project: switchio   Author: friends-of-freeswitch   File: connection.py    License: Mozilla Public License 2.0 6 votes vote down vote up
def api(self, cmd, errcheck=True, block=False, timeout=0.5):
        '''Invoke api command (with error checking by default).
        '''
        if not self.connected():
            raise ConnectionError("Call ``connect()`` first")
        self.log.debug("api cmd '{}'".format(cmd))
        if not block and (get_ident() == self.loop._tid):
            # note this is an `asyncio.Future`
            return self.protocol.api(cmd, errcheck=errcheck)

        # NOTE: this is a `concurrent.futures.Future`
        future = run_in_order_threadsafe(
            [self.protocol.api(cmd, errcheck=errcheck)],
            self.loop,
            timeout=timeout,
            block=block,
        )

        if not block:
            return future

        return future.result(0.005) 
Example 5
Project: ParlAI   Author: facebookresearch   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """
        Returns a singular database connection to be shared amongst all calls.
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 6
Project: ParlAI   Author: facebookresearch   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """
        Returns a singular database connection to be shared amongst all calls.
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 7
Project: ParlAI   Author: facebookresearch   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """
        Returns a singular database connection to be shared amongst all calls.
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 8
Project: PyPtt   Author: PttCodingMan   File: PTT.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _one_thread(self) -> None:
        current_thread_id = threading.get_ident()
        if current_thread_id == self._ThreadID:
            return
        log.show_value(
            self.config,
            log.level.DEBUG,
            'ThreadID',
            self._ThreadID
        )
        log.show_value(
            self.config,
            log.level.DEBUG,
            'Current thread id',
            current_thread_id
        )
        raise exceptions.MultiThreadOperated() 
Example 9
Project: Fluid-Designer   Author: Microvellum   File: base_events.py    License: 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 10
Project: Fluid-Designer   Author: Microvellum   File: base_events.py    License: 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 11
Project: Fluid-Designer   Author: Microvellum   File: test_threaded_import.py    License: 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 12
Project: Fluid-Designer   Author: Microvellum   File: test_signal.py    License: 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 13
Project: Fluid-Designer   Author: Microvellum   File: test_threading.py    License: 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 14
Project: Fluid-Designer   Author: Microvellum   File: test_threading.py    License: 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 15
Project: Fluid-Designer   Author: Microvellum   File: test_capi.py    License: 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 16
Project: HpBandSter   Author: automl   File: worker.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self, background=False):
		"""
		Method to start the worker.
		
		Parameters
		----------
			background: bool
				If set to False (Default). the worker is executed in the current thread.
				If True, a new daemon thread is created that runs the worker. This is
				useful in a single worker scenario/when the compute function only simulates
				work.
		"""
		if background:
			self.worker_id += str(threading.get_ident())
			self.thread = threading.Thread(target=self._run, name='worker %s thread'%self.worker_id)
			self.thread.daemon=True
			self.thread.start()
		else:
			self._run() 
Example 17
Project: Imogen   Author: CedricGuillemet   File: base_events.py    License: MIT License 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 18
Project: neural_chat   Author: natashamjaques   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """Returns a singular database connection to be shared amongst all
        calls
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 19
Project: neural_chat   Author: natashamjaques   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """Returns a singular database connection to be shared amongst all
        calls
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 20
Project: neural_chat   Author: natashamjaques   File: mturk_data_handler.py    License: MIT License 6 votes vote down vote up
def _get_connection(self):
        """Returns a singular database connection to be shared amongst all
        calls
        """
        curr_thread = threading.get_ident()
        if curr_thread not in self.conn or self.conn[curr_thread] is None:
            try:
                conn = sqlite3.connect(self.db_path)
                conn.row_factory = sqlite3.Row
                self.conn[curr_thread] = conn
            except sqlite3.Error as e:
                shared_utils.print_and_log(
                    logging.ERROR,
                    "Could not get db connection, failing: {}".format(repr(e)),
                    should_print=True,
                )
                raise e
        return self.conn[curr_thread] 
Example 21
Project: ironpython3   Author: IronLanguages   File: base_events.py    License: Apache License 2.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 22
Project: ironpython3   Author: IronLanguages   File: base_events.py    License: 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 23
Project: ironpython3   Author: IronLanguages   File: test_threaded_import.py    License: Apache License 2.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 24
Project: ironpython3   Author: IronLanguages   File: test_signal.py    License: Apache License 2.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 25
Project: ironpython3   Author: IronLanguages   File: test_threading.py    License: Apache License 2.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 26
Project: ironpython3   Author: IronLanguages   File: test_threading.py    License: Apache License 2.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 27
Project: ironpython3   Author: IronLanguages   File: test_capi.py    License: Apache License 2.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 28
Project: aioreactive   Author: dbrattli   File: eventloop.py    License: MIT License 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._thread_id = threading.get_ident()
        try:
            while True:
                self._run_once()
                if self._stopping:
                    break
        finally:
            self._stopping = False
            self._thread_id = None 
Example 29
Project: cherrypy   Author: cherrypy   File: cpstats.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_threading_ident():
    if sys.version_info >= (3, 3):
        return threading.get_ident()
    return threading._get_ident() 
Example 30
Project: leaguedirector   Author: RiotGames   File: bindings.py    License: 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()