blackhole.worker

Provides functionality to manage child processes from the supervisor.

class blackhole.worker.Worker(idx, socks, loop=None)[source]

A worker.

Providers functionality to manage a single child process. The worker is responsible for communicating heartbeat information with it’s child process and starting, stopping and restarting a child as required or instructed.

start()[source]

Create and fork off a child process for the current worker.

setup_child()[source]

Set the gid, uid and start the child process..

restart_child()[source]

Restart the child process.

kill_child()[source]

Kill the child process.

async heartbeat(writer)[source]

Handle heartbeat between a worker and child.

If a child process stops communicating with it’s worker, it will be killed, the worker managing it will also be removed and a new worker and child will be spawned.

Parameters

writer (asyncio.StreamWriter) – An object for writing data to the pipe.

Note

3 bytes are used in the communication channel.

The worker will sleep for 15 seconds, before requesting a ping from the child. If we go for over 30 seconds waiting for a ping, the worker will restart itself and the child bound to it.

These message values are defined in the blackhole.protocolsschema. Documentation is available at – https://kura.gg/blackhole/api-protocols.html

async chat(reader)[source]

Communicate between a worker and child.

If communication with the child fails the worker is shutdown and the child is killed.

Parameters

reader (asyncio.StreamReader) – An object for reading data from the pipe.

Note

3 bytes are used in the communication channel.

Read data coming in from the child. If a PONG is received, we’ll update the worker, setting this PONG as a ‘PING’ from the child.

These message values are defined in the blackhole.protocolsschema. Documentation is available at – https://kura.gg/blackhole/api-protocols.html

async connect()[source]

Connect the child and worker so they can communicate.

Parameters
  • up_write (int) – A file descriptor.

  • down_read (int) – A file descriptor.

stop()[source]

Terminate the worker and it’s respective child process.