def serve_from_child(sock, config, controller_pid): threads = config.get('threadpool_workers', 0) wsgi_application = api.named(config['app_factory'])(config) if config.get('coverage'): wsgi_application = FigleafCoverage(wsgi_application) if threads > 1: wsgi_application = ExecuteInThreadpool(wsgi_application) elif threads != 1: print "(%s) not using threads, installing eventlet cooperation monkeypatching" % ( os.getpid(), ) from eventlet import util util.wrap_socket_with_coroutine_socket() #util.wrap_pipes_with_coroutine_pipes() #util.wrap_threading_local_with_coro_local() host, port = sock.getsockname() access_log_file = config.get('access_log_file') if access_log_file is not None: access_log_file = open(access_log_file, 'a') max_age = 0 if config.get('max_age'): max_age = int(config.get('max_age')) server_event = coros.event() http_version = config.get('no_keepalive') and 'HTTP/1.0' or 'HTTP/1.1' try: wsgi_args = (sock, wsgi_application) wsgi_kwargs = {'log' : access_log_file, 'server_event' : server_event, 'max_http_version' : http_version} if config.get('no_keepalive'): wsgi_kwargs.update({'keepalive' : False}) if max_age: wsgi_kwargs.update({'timeout_value' : True}) api.with_timeout(max_age, wsgi.server, *wsgi_args, **wsgi_kwargs) else: wsgi.server(*wsgi_args, **wsgi_kwargs) except KeyboardInterrupt: pass except ExitChild: pass ## Set a deadman timer to violently kill the process if it doesn't die after ## some long timeout. signal.signal(signal.SIGALRM, deadman_timeout) signal.alarm(config['deadman_timeout']) ## Once we get here, we just need to handle outstanding sockets, not ## accept any new sockets, so we should close the server socket. sock.close() server = server_event.wait() last_outstanding = None if server.outstanding_requests: ## Let's tell our parent that we're dying try: os.kill(controller_pid, signal.SIGUSR1) except OSError, e: if not e.errno == errno.ESRCH: raise
#!/usr/bin/env python # encoding: utf-8 import sys, os sys.path[0:0] = [ os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'lib') ] from eventlet import api, coros, util util.wrap_socket_with_coroutine_socket() from fcgiev.fcgi import handle_connection def fcgi_stdin_sock(): import socket from eventlet.wrappedfd import wrapped_fd return wrapped_fd(socket.fromfd(0, socket.AF_UNIX, socket.SOCK_STREAM)) def accept(sock, handler): try: print 'listening on', sock.getsockname() while True: try: handler(*sock.accept()) except KeyboardInterrupt: api.get_hub().remove_descriptor(sock.fileno()) break finally: try: sock.close() except socket.error: pass
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import os from eventlet.green import socket from eventlet.green import httplib from eventlet.pools import Pool from eventlet.util import wrap_socket_with_coroutine_socket wrap_socket_with_coroutine_socket() def make_proxy_connection(uri): headers = headers or {} proxy = None if uri.scheme == 'https': proxy = os.environ.get('https_proxy') elif uri.scheme == 'http': proxy = os.environ.get('http_proxy') if not proxy: return make_connection(uri, use_proxy=False) if uri.scheme == 'https': proxy_auth = _get_proxy_auth() if proxy_auth:
feeds = ( {'domain':'IA', 'url':'http://bookserver.archive.org/catalog/crawlable', 'sorted_by_date':False}, #{'domain':'OReilly', 'url':'http://catalog.oreilly.com/stanza/alphabetical.xml', 'sorted_by_date':False}, #{'domain':'Feedbooks', 'url':'http://www.feedbooks.com/discover/authors.atom', 'sorted_by_date':False}, ) import feedparser #import feedparser before eventlet from eventlet import coros, httpc, util ## From the eventlet examples page: # replace socket with a cooperative coroutine socket because httpc # uses httplib, which uses socket. Removing this serializes the http # requests, because the standard socket is blocking. util.wrap_socket_with_coroutine_socket() import commands import os import time import glob import re import tempfile import Queue import string import datetime import xml.utils.iso8601 import time import socket
def green_init(): from eventlet.util import wrap_socket_with_coroutine_socket wrap_socket_with_coroutine_socket()