guv.hubs.switch - facilities for cooperative yielding

guv.hubs.switch.gyield(switch_back=True)[source]

Yield to other greenlets

This is a cooperative yield which suspends the current greenlet and allows other greenlets to run by switching to the hub.

  • If switch_back is True (default), the current greenlet is resumed at the beginning of the next event loop iteration, before the loop polls for I/O and calls any I/O callbacks. This is the intended use for this function the vast majority of the time.
  • If switch_back is False, the hub will will never resume the current greenlet (use with caution). This is mainly useful for situations where other greenlets (not the hub) are responsible for switching back to this greenlet. An example is the Event class, where waiters are switched to when the event is ready.
Parameters:switch_back (bool) – automatically switch back to this greenlet on the next event loop cycle
guv.hubs.switch.trampoline(fd, evtype, timeout=None, timeout_exc=<class 'guv.timeout.Timeout'>)[source]

Jump from the current greenlet to the hub and wait until the given file descriptor is ready for I/O, or the specified timeout elapses

If the specified timeout elapses before the socket is ready to read or write, timeout_exc will be raised instead of trampoline() returning normally.

When the specified file descriptor is ready for I/O, the hub internally calls the callback to switch back to the current (this) greenlet.

Conditions:

  • must not be called from the hub greenlet (can be called from any other greenlet)
  • evtype must be either READ or WRITE (not possible to watch for both simultaneously)
Parameters:
  • fd (int) – file descriptor
  • evtype (int) – either the constant READ or WRITE
  • timeout (float) – (optional) maximum time to wait in seconds
  • timeout_exc (Exception) – (optional) timeout Exception class