API

magnetic

A bunch of tools for using venvs (and virtualenvs) from python.

copyright:
  1. 2015 by James Tocknell.
license:

MIT, see LICENSE for more details.

magnetic.get_activated_sockets(method, max_socks=None)[source]

Returns max_socks number of sockets passed to python via method.

Parameters:
  • method (str or None) – The system used to perform socket activation. None specifies that a new socket be created. Supported values are ‘systemd’, ‘inetd’, ‘launchd’, and None.
  • max_socks (int or None) – Maximum number of sockets that should be acquired. If the number of sockets that can be acquired exceeds this, raise magnetic.MagneticError. None disables this check.
Returns:

The acquired sockets.

Return type:

list of socket.socket or equivalent

Raises:

magnetic.MagneticError – if socket acquisition fails.

magnetic.get_socket(method, family=None, type=None, proto=None, addr=None, force_create=False)[source]

Utility function to get a single socket-activation socket.

get_socket returns a single socket (socket.socket), with bind having been called by the parent process (or equivalent depending on system). In the case that acquiring the socket fails, magnetic.MagneticError is raised. If force_create is True, try to create a socket with family, type and proto.

Note

This function enforces that a single socket be returned, multiple sockets being provided cause this function to raise magnetic.MagneticError.

Warning

There should only be a single call to get_socket or get_sockets. Multiple calls may break in strange ways.

Parameters:
  • method (str or None) – The system used to perform socket activation. None specifies that a new socket be created. Supported values are ‘systemd’, ‘inetd’, ‘launchd’, ‘upstart’ and None.
  • family (socket.AF_* or similar) – The family used if a new socket is created. See socket.socket for further information.
  • type (socket.SOCK_* or similar) – The type used if a new socket is created. See socket.socket for further information.
  • protocol (int) – The protocol used if a new socket is created. See socket.socket for further information.
  • bool (force_create) – Create a new socket if activation fails.
Returns:

The acquired socket.

Return type:

socket.socket or equivalent

Raises:

magnetic.MagneticError – if socket acquisition fails.

magnetic.get_sockets(method, families=None, types=None, protos=None, addrs=None, force_create=False, max_socks=None)[source]

Utility function to get socket-activation sockets.

get_sockets returns a list of sockets (socket.socket), with bind having been called by the parent process (or equivalent depending on system). In the case that acquiring the sockets fail, magnetic.MagneticError is raised. If force_create is True, try to create sockets with families, types and protos.

Warning

There should only be a single call to get_socket or get_sockets. Multiple calls may break in strange ways.

Parameters:
  • method (str or None) – The system used to perform socket activation. None specifies that a new socket be created. Supported values are ‘systemd’, ‘inetd’, ‘launchd’, and None.
  • families (list of socket.AF_* or similar) – List of families to use if new sockets are created. Each item represents a new socket. See socket.socket for further information.
  • types (list of socket.SOCK_* or similar) – List of types to use if new sockets are created. Each item represents a new socket. See socket.socket for further information.
  • protocol (list of int) – List of families to use if new sockets are created. Each item represents a new socket. See socket.socket for further information.
  • bool (force_create) – Create a new socket if activation fails.
  • max_socks (int or None) – Maximum number of sockets that should be acquired. If the number of sockets that can be acquired exceeds this, raise magnetic.MagneticError. None disables this check.
Returns:

The acquired sockets.

Return type:

list of socket.socket or equivalent

Raises:

magnetic.MagneticError – if socket acquisition fails.

class magnetic.asyncore.dispatcher(sock=None, map=None)[source]

Subclass of asyncore.dispatcher with socket activation support.

This class provides a version of asyncore.dispatcher with socket activation support via the sock_activate_method attribute. Defining this attribute (see get_socket‘s method) will cause the dispatcher to acquire a socket from that system, instead of creating its own. By default, not defining sock_activate_method causes the dispatcher to create its own socket.

class magnetic.asyncore.dispatcher_with_send(sock=None, map=None)[source]

Subclass of asyncore.dispatcher_with_send with socket activation support.

This class provides a version of asyncore.dispatcher_with_send with socket activation support via the sock_activate_method attribute. Defining this attribute (see get_socket‘s method) will cause the dispatcher_with_send to acquire a socket from that system, instead of creating its own. By default, not defining sock_activate_method causes the dispatcher_with_send to create its own socket.