Package coro :: Module _coro
[hide private]
[frames] | no frames]

Module _coro

Pyrex module for coroutine implementation.

Module variables defined below are available only from Pyrex. Python-accessible variables are documented in the top level of the coro package __init__.py.

Classes [hide private]
  AF
Socket families.
  ClosedError
Another thread closed this descriptor.
  DeadCoroutine
attempt to resume a dead coroutine
  EV
Kevent flags.
  EVFILT
Kevent filters.
  Exit
exception used to exit the event loop
  Interrupted
A coroutine has been interrupted unexpectedly
  LockError
  NOTE
Kevent filter flags.
  NotStartedError
Attempted to interrupt a thread before it has started.
  PF
Socket families.
  SHUT
Socket shutdown methods.
  SO
Socket options.
  SOCK
Socket types.
  SOL
Socket levels.
  ScheduleError
attempt to schedule an already-scheduled coroutine
  Shutdown
The thread is shutting down.
  SimultaneousError
Two threads attempted a conflicting blocking operation (e.g., read() on the same descriptor).
  SysctlError
  ThreadLocal
Thread Local Storage.
  TimeoutError
A call to with_timeout() has expired
  WakeUp
A convenience exception used to wake up a sleeping thread.
  _fifo
  _fifo_iterator
  _profiler
The profiler object.
  aio_control_block
Wrapper for 'struct aiocb'.
  bench
Base benchmarking class.
  c_function
C function.
  call_counts_object
Track caller->callee call counts.
  condition_variable
Condition variable.
  coro
XXX
  coro_fd
A file-object wrapper using the socket object.
  event
  event_queue
  event_queue_iter
  exception
used to interrupt coroutines with an exception
  fd_sock
A file-descriptor wrapper using the socket object.
  fifo
First-in First-Out container.
  file_sock
A file-object wrapper using the socket object.
  function
Function representation for the profiler.
  internal_function
Internal function.
  inverted_semaphore
Inverted semaphore.
  kevent_key
Kevent key.
  kevent_target
  mutex
Mutual Exclusion lock object.
  node
  py_kevent
Representation of a kevent.
  python_function
Python function.
  queue_poller
  rusage
  rusage_bench
Subclass of bench object that tracks rusage data.
  rw_lock
A many-reader single-writer lock.
  sched
  semaphore
Semaphore lock object.
  sock
Coro socket object.
  timebomb
Functions [hide private]
 
P(...)
Print a string to stderr with a timestamp.
 
_yield(...)
 
aio_read(...)
Asynchronously read data from a file.
 
aio_stats(...)
Return AIO statistics.
 
aio_write(...)
Asynchronously write data to a file.
 
current(...)
Return the current coroutine object.
 
default_exception_notifier(...)
 
event_loop(...)
Start the event loop.
source code
 
get_live_coros(...)
Get the number of live coroutines.
 
get_live_sockets(...)
Get the number of live socket objects.
 
get_module_name(...)
 
get_refcnt(...)
 
get_the_profiler(...)
 
has_ipv6(...)
Whether or not this system can create an IPv6 socket.
 
make_socket(...)
Create a socket object.
 
new(...)
Create a new coroutine object.
 
new_profiler(...)
 
print_stderr(...)
Print a string to stderr with a timestamp.
 
schedule(...)
Schedule a coroutine to run.
 
set_exception_notifier(...)
Set the exception notifier.
 
set_exit(...)
Indicate that the event loop should exit.
 
set_handler(...)
Add a kevent handler.
source code
 
set_latency_warning(...)
Set the latency warning threshold multiplier.
source code
 
set_print_exit_string(...)
Set whether or not "Exiting" should be printed when the event loop exits.
 
set_selfishness(...)
 
sleep_absolute(...)
This is an alias for the sleep method.
source code
 
sleep_relative(...)
Sleep for a period of time.
source code
 
socketpair(...)
Create an unnamed pair of connected sockets.
 
spawn(...)
Spawn a new coroutine.
 
tcp6_sock(...)
Create a streaming IPv6 socket.
 
tcp_sock(...)
Create a streaming IPv4 socket.
 
udp6_sock(...)
Create a datagram IPv6 socket.
 
udp_sock(...)
Create a datagram IPv4 socket.
 
unix_sock(...)
Create a streaming unix-domain socket.
 
wait_for(...)
Wait for an event.
source code
 
with_timeout(...)
Call a function with a timeout.
source code
 
yield_slice(...)
Yield to allow other threads to run.
Variables [hide private]
  __aio_version__ = '$Id: //prod/main/ap/shrapnel/coro/aio.pyx#1...
  __coro_version__ = '$Id: //prod/main/ap/shrapnel/coro/_coro.py...
  __event_queue_version__ = '$Id: event_queue.pyx,v 1.1 2007/01/...
  __package__ = 'coro'
  __poller_version__ = '$Id: //prod/main/ap/shrapnel/coro/poller...
  __profile_version__ = '$Id: //prod/main/ap/shrapnel/coro/profi...
  __socket_version__ = '$Id: //prod/main/ap/shrapnel/coro/socket...
  __sync_version__ = '$Id: //prod/main/ap/shrapnel/coro/sync.pyx...
  __test__ = {}
  __versions__ = ['$Id: //prod/main/ap/shrapnel/coro/_coro.pyx#1...
  _print_exit_string = True
  _the_scheduler = <coro._coro.sched object at 0x8027747d0>
  _ticks_per_sec
: Number of CPU ticks per second (uint64_t).
  all_threads = {}
  event_map = {}
  saved_stderr = <epydoc.docintrospecter._DevNull instance at 0x...
Function Details [hide private]

P(...)

 

Print a string to stderr with a timestamp.

This will print the thread id, followed by a timestamp, followed by the string. If the string does not end with a newline, one will be added.

Parameters:
  • s - : A string to print.

aio_read(...)

 
Asynchronously read data from a file.
Parameters:
  • fd - : The file descriptor to read from.
  • nbytes - : The number of bytes to read.
  • offset - : The offset to read from.
Returns:
Returns a string of data read from the file.
Raises:
  • OSError - : OS-level error.

aio_stats(...)

 
Return AIO statistics.
Returns:

Returns a tuple of (rnb, rb, wnb, wb, rp, wp):

  • rnb: Non-blocking reads.
  • rb: Blocking-reads.
  • wnb: Non-blocking writes.
  • wb: Blocking-writes.
  • rp: Pending reads.
  • wp: Pending writes.

aio_write(...)

 
Asynchronously write data to a file.
Parameters:
  • fd - : The file descriptor to write to.
  • buffer - : String data to write.
  • offset - : The offset to write the data.
Returns:
Returns the number of bytes written.
Raises:
  • OSError - : OS-level error.

event_loop(...)

source code 
Start the event loop.
Parameters:
  • timeout - : The amount of time to wait for kevent to return events. You should probably not set this value. Defaults to 30 seconds.

get_live_coros(...)

 

Get the number of live coroutines.

Note that this includes coroutines that have not started or have exited, but not deallocated, yet.

Returns:
Returns the number of live coroutine objects.

get_live_sockets(...)

 
Get the number of live socket objects. This includes socket objects that are closed.
Returns:
Returns the number of socket objects.

has_ipv6(...)

 
Whether or not this system can create an IPv6 socket.
Returns:
Returns True if this system can create an IPv6 socket, False otherwise

Exceptions:

  • None

make_socket(...)

 

Create a socket object.

This is a backwards-compatibility wrapper around the sock object constructor.

Parameters:
  • domain - : The socket domain family (see AF).
  • stype - : The socket type (see SOCK).
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

new(...)

 

Create a new coroutine object.

Additional arguments and keyword arguments will be passed to the given function.

This will not start the coroutine. Call the start method on the coroutine to schedule it to run.

Parameters:
  • fun - : The function to call when the coroutine starts.
Returns:
Returns the new coroutine object.

print_stderr(...)

 

Print a string to stderr with a timestamp.

This will print the thread id, followed by a timestamp, followed by the string. If the string does not end with a newline, one will be added.

Parameters:
  • s - : A string to print.

schedule(...)

 

Schedule a coroutine to run.

See coro.schedule for more detail.

Parameters:
  • co - : The coroutine to schedule.
  • value - : The value to resume the coroutine with. Defaults to None.

set_exception_notifier(...)

 

Set the exception notifier.

The exception notifier is a function that is called when a coroutine exits due to an exception. The default exception notifier simply prints the name of the coroutine and a traceback of where the exception was raised.

Parameters:
  • new_func - : The exception notifier to call. It takes no arguments.
Returns:
Returns the old exception notifier.

set_exit(...)

 

Indicate that the event loop should exit.

Note that if any other coroutines are scheduled to run, they will be given a chance to run before exiting.

Parameters:
  • exit_code - : The exit code of the process. Defaults to 0.

set_handler(...)

source code 

Add a kevent handler.

This is a low-level interface to register a kevent handler.

Parameters:
  • event - : A tuple of (ident, filter) of the kevent to handle.
  • handler - : The handler to use, a callable object which will be called with one argument, a py_kevent object.
  • flags - : Kevent flags to use. Defaults to EV_ADD|EV_ONESHOT.
  • fflags` - : Kevent filter flags to use. Defaults to 0.
Raises:

set_latency_warning(...)

source code 

Set the latency warning threshold multiplier.

The default latency warning threshold is 0.2 seconds. This will allow you to change the threshold by multiplying the 0.2 value.

Parameters:
  • factor - : The latency threshold multiplier. May be a number from 0 to 300. A value of 0 disables latency warnings.
Returns:
Returns the old multipler factor.
Raises:
  • ValueError - : The factor is too small or too large.

set_print_exit_string(...)

 

Set whether or not "Exiting" should be printed when the event loop exits.

By default, the string will be printed.

Parameters:
  • val - : Whether or not "Exiting" should be printed when the event loop exits.

sleep_relative(...)

source code 

Sleep for a period of time.

If a thread is interrupted at the exact same time the sleep is finished, it is not defined whether the interrupt or the sleep "wins". Your thread may continue running (with the interrupt rescheduled to try again later), or it may be interrupted.

Parameters:
  • delta - : The number of seconds to sleep.

socketpair(...)

 
Create an unnamed pair of connected sockets.
Parameters:
  • domain - : The socket domain family (defaults to AF_UNIX).
  • stype - : The socket type (defaults to SOCK_STREAM).
  • protocol - : The socket protocol (normally not used, defaults to 0).
Returns:
Returns a tuple of 2 connected sockets.
Raises:
  • OSError - : OS-level error.

spawn(...)

 

Spawn a new coroutine.

Additional arguments and keyword arguments will be passed to the given function.

Parameters:
  • fun - : The function to call when the coroutine starts.
Returns:
Returns the new coroutine object.

tcp6_sock(...)

 
Create a streaming IPv6 socket.
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

tcp_sock(...)

 
Create a streaming IPv4 socket.
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

udp6_sock(...)

 
Create a datagram IPv6 socket.
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

udp_sock(...)

 
Create a datagram IPv4 socket.
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

unix_sock(...)

 
Create a streaming unix-domain socket.
Returns:
Returns a socket object.
Raises:
  • OSError - : OS-level error.

wait_for(...)

source code 
Wait for an event.
Parameters:
  • ident - : The kevent identifier (depends on the filter type, but is often a file descriptor).
  • filter - : The kevent filter (see EVFILT).
  • fflags - : Filter flags (defaults to 0).
Returns:
Returns a py_kevent instance that indicates the event that fired.
Raises:

with_timeout(...)

source code 

Call a function with a timeout.

Additional arguments and keyword arguments provided are passed to the function. This will re-raise any exceptions raised by the function.

If a timeout expires, but the function returns before the next pass in the event loop, then the timeout will be diffused.

If a coroutine is already scheduled to run (such as if it received a kevent), and the timeout expires, the timeout will be put on "hold" to let the coroutine run and process the data. If the function returns, then the timeout will be defused, otherwise the timeout will be given another chance to fire during the next pass through the event loop. One should note that due to this behavior, if a coroutine is continually receiving kevents, the timeout will never fire until the kevents stop.

Nested timeouts will be handled correctly. If an outer timeout fires first, then only the outer except TimeoutError exception handler will catch it. An exception handlers on the inside will be skipped becaue the actual exception is the Interrupted exception until it gets to the original with_timeout frame.

Nested timeouts that are set to fire at the exact same time are not defined which one will fire first.

Care must be taken to never catch the Interrupted exception within code that is wrapped with a timeout.

Returns:
Returns the return value of the function.
Raises:
  • TimeoutError - : The function did not return within the specified timeout.

Parameters:

  • delta: The number of seconds to wait before raising a timeout.

    Should be >= 0. Negative value will be treated as 0.

  • function: The function to call.

yield_slice(...)

 

Yield to allow other threads to run.

This will yield to allow other threads to run. The coroutine will be rescheduled to run during the next pass through the event loop.


Variables Details [hide private]

__aio_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/aio.pyx#19 $'

__coro_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/_coro.pyx#114 $'

__event_queue_version__

Value:
'$Id: event_queue.pyx,v 1.1 2007/01/03 00:19:50 ehuss Exp $'

__poller_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/poller.pyx#46 $'

__profile_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/profile.pyx#15 $'

__socket_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/socket.pyx#57 $'

__sync_version__

Value:
'$Id: //prod/main/ap/shrapnel/coro/sync.pyx#29 $'

__versions__

Value:
['$Id: //prod/main/ap/shrapnel/coro/_coro.pyx#114 $',
 '$Id: //prod/main/ap/shrapnel/coro/socket.pyx#57 $',
 '$Id: //prod/main/ap/shrapnel/coro/sync.pyx#29 $',
 '$Id: event_queue.pyx,v 1.1 2007/01/03 00:19:50 ehuss Exp $',
 '$Id: //prod/main/ap/shrapnel/coro/poller.pyx#46 $',
 '$Id: //prod/main/ap/shrapnel/coro/profile.pyx#15 $',
 '$Id: //prod/main/ap/shrapnel/coro/aio.pyx#19 $']

saved_stderr

Value:
<epydoc.docintrospecter._DevNull instance at 0x80272ac20>