gevent.thread – Implementation of the standard thread module that spawns greenlets

Implementation of the standard thread module that spawns greenlets.

Note

This module is a helper for gevent.monkey and is not intended to be used directly. For spawning greenlets in your applications, prefer higher level constructs like gevent.Greenlet class or gevent.spawn().

error

alias of RuntimeError

class LockType[source]

Bases: _AtomicBoundedSemaphore

The basic lock type.

Changed in version 24.10.1: Subclassing this object is no longer allowed. This matches the Python 3 API.

Changed in version 24.10.1: No longer accepts arguments to pass to the super class. If you want a semaphore with a different count, use a semaphore class directly. This matches the Lock API of Python 3

acquire(blocking=True, timeout=None) bool[source]

Acquire the semaphore.

Note

If this semaphore was initialized with a value of 0, this method will block forever (unless a timeout is given or blocking is set to false).

Parameters:
  • blocking (bool) – If True (the default), this function will block until the semaphore is acquired.

  • timeout (float) – If given, and blocking is true, specifies the maximum amount of seconds this method will block.

Returns:

A bool indicating whether the semaphore was acquired. If blocking is True and timeout is None (the default), then (so long as this semaphore was initialized with a size greater than 0) this will always return True. If a timeout was given, and it expired before the semaphore was acquired, False will be returned. (Note that this can still raise a Timeout exception, if some other caller had already started a timer.)

allocate_lock

alias of LockType

lock

alias of LockType

allocate()

An obsolete synonym of allocate_lock().

daemon_threads_allowed()

Return True if daemon threads are allowed in the current interpreter, and False otherwise.

exit_thread()

An obsolete synonym of exit().

get_native_id()

Return a non-negative integer identifying the thread as reported by the OS (kernel). This may be used to uniquely identify a particular thread within a system.

interrupt_main(signum=Signals.SIGINT, /)

Simulate the arrival of the given signal in the main thread, where the corresponding signal handler will be executed. If signum is omitted, SIGINT is assumed. A subthread can use this function to interrupt the main thread.

Note: the default signal handler for SIGINT raises KeyboardInterrupt.

start_joinable_thread(function, handle=None, daemon=True)[source]

For internal use only: start a new thread.

Like start_new_thread(), this starts a new thread calling the given function. Unlike start_new_thread(), this returns a handle object with methods to join or detach the given thread. This function is not for third-party code, please use the threading module instead. During finalization the runtime will not wait for the thread to exit if daemon is True. If handle is provided it must be a newly created thread._ThreadHandle instance.

start_new(function, args, kwargs={}, /)

An obsolete synonym of start_new_thread().