# Path: tornado/ioloop.py
# class IOLoop(Configurable):
# class PollIOLoop(IOLoop):
# class _Timeout(object):
# class PeriodicCallback(object):
#     _EPOLLIN = 0x001
#     _EPOLLPRI = 0x002
#     _EPOLLOUT = 0x004
#     _EPOLLERR = 0x008
#     _EPOLLHUP = 0x010
#     _EPOLLRDHUP = 0x2000
#     _EPOLLONESHOT = (1 << 30)
#     _EPOLLET = (1 << 31)
#     NONE = 0
#     READ = _EPOLLIN
#     WRITE = _EPOLLOUT
#     ERROR = _EPOLLERR | _EPOLLHUP
#         _FUTURE_TYPES = (futures.Future, DummyFuture)
#         _FUTURE_TYPES = DummyFuture
#     def instance():
#     def initialized():
#     def install(self):
#     def current():
#     def make_current(self):
#     def clear_current(self):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def set_blocking_log_threshold(self, seconds):
#     def log_stack(self, signal, frame):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def add_future(self, future, callback):
#     def _run_callback(self, callback):
#     def handle_callback_exception(self, callback):
#     def initialize(self, impl, time_func=None):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def __init__(self, deadline, callback, io_loop):
#     def timedelta_to_seconds(td):
#     def __lt__(self, other):
#     def __le__(self, other):
#     def __init__(self, callback, callback_time, io_loop=None):
#     def start(self):
#     def stop(self):
#     def _run(self):
#     def _schedule_next(self):
# 
# Path: tornado/iostream.py
# class PipeIOStream(BaseIOStream):
#     """Pipe-based IOStream implementation.
# 
#     The constructor takes an integer file descriptor (such as one returned
#     by `os.pipe`) rather than an open file object.
#     """
#     def __init__(self, fd, *args, **kwargs):
#         self.fd = fd
#         _set_nonblocking(fd)
#         super(PipeIOStream, self).__init__(*args, **kwargs)
# 
#     def fileno(self):
#         return self.fd
# 
#     def close_fd(self):
#         os.close(self.fd)
# 
#     def write_to_fd(self, data):
#         return os.write(self.fd, data)
# 
#     def read_from_fd(self):
#         try:
#             chunk = os.read(self.fd, self.read_chunk_size)
#         except (IOError, OSError), e:
#             if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
#                 return None
#             elif e.args[0] == errno.EBADF:
#                 # If the writing half of a pipe is closed, select will
#                 # report it as readable but reads will fail with EBADF.
#                 self.close()
#                 return None
#             else:
#                 raise
#         if not chunk:
#             self.close()
#             return None
#         return chunk
# 
# Path: tornado/log.py
# def _stderr_supports_color():
#     def __init__(self, color=True, *args, **kwargs):
#     def format(self, record):
# def enable_pretty_logging(options=None):
# def define_logging_options(options=None):
# class LogFormatter(logging.Formatter):
# 
# Path: tornado/stack_context.py
# class _State(threading.local):
# class StackContext(object):
# class ExceptionStackContext(object):
# class NullContext(object):
# class _StackContextWrapper(functools.partial):
#     def __init__(self):
#     def __init__(self, context_factory, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __init__(self, exception_handler, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def wrap(fn):
#     def wrapped(*args, **kwargs):
# def _nested(*managers):


import errno
import functools
import os
import signal
import subprocess
import sys
import time
    import multiprocessing  # Python 2.6+
    import random
from binascii import hexlify
from tornado import ioloop
from tornado.iostream import PipeIOStream
from tornado.log import gen_log
from tornado import stack_context
    gen_log.info("Starting %d processes", num_processes)
    children = {}

    def start_child(i):
        pid = os.fork()
        if pid == 0:
            # child process
            _reseed_random()
            global _task_id
            _task_id = i
            return i
        else:
            children[pid] = i
            return None
    for i in range(num_processes):
        id = start_child(i)
        if id is not None:
            return id
    num_restarts = 0
    while children:
        try:
            pid, status = os.wait()
        except OSError, e:
            if e.errno == errno.EINTR:
                continue
            raise
        if pid not in children:
            continue
        id = children.pop(pid)
        if os.WIFSIGNALED(status):

# Path: tornado/ioloop.py
# class IOLoop(Configurable):
# class PollIOLoop(IOLoop):
# class _Timeout(object):
# class PeriodicCallback(object):
#     _EPOLLIN = 0x001
#     _EPOLLPRI = 0x002
#     _EPOLLOUT = 0x004
#     _EPOLLERR = 0x008
#     _EPOLLHUP = 0x010
#     _EPOLLRDHUP = 0x2000
#     _EPOLLONESHOT = (1 << 30)
#     _EPOLLET = (1 << 31)
#     NONE = 0
#     READ = _EPOLLIN
#     WRITE = _EPOLLOUT
#     ERROR = _EPOLLERR | _EPOLLHUP
#         _FUTURE_TYPES = (futures.Future, DummyFuture)
#         _FUTURE_TYPES = DummyFuture
#     def instance():
#     def initialized():
#     def install(self):
#     def current():
#     def make_current(self):
#     def clear_current(self):
#     def configurable_base(cls):
#     def configurable_default(cls):
#     def initialize(self):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def set_blocking_log_threshold(self, seconds):
#     def log_stack(self, signal, frame):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def add_future(self, future, callback):
#     def _run_callback(self, callback):
#     def handle_callback_exception(self, callback):
#     def initialize(self, impl, time_func=None):
#     def close(self, all_fds=False):
#     def add_handler(self, fd, handler, events):
#     def update_handler(self, fd, events):
#     def remove_handler(self, fd):
#     def set_blocking_signal_threshold(self, seconds, action):
#     def start(self):
#     def stop(self):
#     def time(self):
#     def add_timeout(self, deadline, callback):
#     def remove_timeout(self, timeout):
#     def add_callback(self, callback, *args, **kwargs):
#     def add_callback_from_signal(self, callback, *args, **kwargs):
#     def __init__(self, deadline, callback, io_loop):
#     def timedelta_to_seconds(td):
#     def __lt__(self, other):
#     def __le__(self, other):
#     def __init__(self, callback, callback_time, io_loop=None):
#     def start(self):
#     def stop(self):
#     def _run(self):
#     def _schedule_next(self):
# 
# Path: tornado/iostream.py
# class PipeIOStream(BaseIOStream):
#     """Pipe-based IOStream implementation.
# 
#     The constructor takes an integer file descriptor (such as one returned
#     by `os.pipe`) rather than an open file object.
#     """
#     def __init__(self, fd, *args, **kwargs):
#         self.fd = fd
#         _set_nonblocking(fd)
#         super(PipeIOStream, self).__init__(*args, **kwargs)
# 
#     def fileno(self):
#         return self.fd
# 
#     def close_fd(self):
#         os.close(self.fd)
# 
#     def write_to_fd(self, data):
#         return os.write(self.fd, data)
# 
#     def read_from_fd(self):
#         try:
#             chunk = os.read(self.fd, self.read_chunk_size)
#         except (IOError, OSError), e:
#             if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
#                 return None
#             elif e.args[0] == errno.EBADF:
#                 # If the writing half of a pipe is closed, select will
#                 # report it as readable but reads will fail with EBADF.
#                 self.close()
#                 return None
#             else:
#                 raise
#         if not chunk:
#             self.close()
#             return None
#         return chunk
# 
# Path: tornado/log.py
# def _stderr_supports_color():
#     def __init__(self, color=True, *args, **kwargs):
#     def format(self, record):
# def enable_pretty_logging(options=None):
# def define_logging_options(options=None):
# class LogFormatter(logging.Formatter):
# 
# Path: tornado/stack_context.py
# class _State(threading.local):
# class StackContext(object):
# class ExceptionStackContext(object):
# class NullContext(object):
# class _StackContextWrapper(functools.partial):
#     def __init__(self):
#     def __init__(self, context_factory, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __init__(self, exception_handler, _active_cell=None):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
#     def __enter__(self):
#     def __exit__(self, type, value, traceback):
# def wrap(fn):
#     def wrapped(*args, **kwargs):
# def _nested(*managers):


# Path: tornado/process.py
import errno
import functools
import os
import signal
import subprocess
import sys
import time
    import multiprocessing  # Python 2.6+
    import random
from binascii import hexlify
from tornado import ioloop
from tornado.iostream import PipeIOStream
from tornado.log import gen_log
from tornado import stack_context

    gen_log.info("Starting %d processes", num_processes)
    children = {}

    def start_child(i):
        pid = os.fork()
        if pid == 0:
            # child process
            _reseed_random()
            global _task_id
            _task_id = i
            return i
        else:
            children[pid] = i
            return None
    for i in range(num_processes):
        id = start_child(i)
        if id is not None:
            return id
    num_restarts = 0
    while children:
        try:
            pid, status = os.wait()
        except OSError, e:
            if e.errno == errno.EINTR:
                continue
            raise
        if pid not in children:
            continue
        id = children.pop(pid)
        if os.WIFSIGNALED(status):

            gen_log.warning("child %d (pid %d) killed by signal %d, restarting",