If your character
it was his idea to
Fear of the Unknow
Work From Home, Ho
Off With Their Hea
My favorite, and e
The last mile is c
I'm No Dummy
About to Have a Ru
Not the Only ActorA simple way of describing this would be this: when the
# user hits a certain point in the program (typically the first
# request for a url), we will execute certain functions at certain
# times, as determined by the following config variables.
#
# The functions are of two kinds: 'request_handlers' and 'statsd_publishers'
#
# - 'request_handlers' are functions that will be called to actually make a
# real http request, and have their return value interpreted as the request's
# result code (see documentation of 'get_user_or_error').
# - 'statsd_publishers' are functions that will be called to store some statistical
# info.
#
# Request handlers are run in the order they appear in the config file, and once a
# request handler is started, it will run continuously (and its result will be
# cached, for subsequent requests). Request handlers can safely call each other,
# which makes it possible to have inter-request handlers (e.g., a request handler
# that reads some data from a file, then makes a request using the data just read,
# and then writes the data to another file).
#
# The idea of using request handlers rather than just writing code into the main
# thread is that, once one or more request handlers are started, the main thread is
# free to do other things (e.g., start background workers, or simply enter an
# endless loop).
log.debug('Starting http_server')
for url, handler in config.http_handlers:
log.debug("Start http server for %s", url)
http_server = create_simple_server(handler)
http_server.serve_forever()
log.debug('Starting statsd_server')
for statsd_url in config.statsd_urls:
log.debug("Start statsd server for %s", statsd_url)
statsd_server = create_simple_server(statsd_publisher,
statsd_url)
statsd_server.serve_forever()
log.debug('Done')
if not config.max_requests:
return
# Start a new thread that will limit the number of requests to a small fixed number.
def limit_requests():
while True:
try:
if config.active_requests >= config.max_requests:
break
except KeyboardInterrupt:
pass
if hasattr(threading, 'current_thread'):
# On Py 2.7 and Py 3.3, threading.current_thread is a thread,
# so let's use that as the new thread:
limit_requests()
else:
limit_requests.start()
# On Py 2.6, threading.current_thread is None, so let's create
# the new thread ourselves:
limit_requests.join()
if __name__ == '__main__':
main()