Created
September 5, 2013 03:55
-
-
Save 4ft35t/6445902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding:utf-8 | |
import sys | |
import os | |
try: | |
import gevent | |
import gevent.queue | |
import gevent.monkey | |
import gevent.coros | |
import gevent.server | |
import gevent.pool | |
gevent.monkey.patch_all(dns=gevent.version_info[0]>=1) | |
except ImportError: | |
sys.stderr.write('WARNING: python-gevent(http://code.google.com/p/gevent/downloads/list) not installed.\n') | |
import Queue | |
import thread | |
import threading | |
import SocketServer | |
def GeventImport(name): | |
import sys | |
sys.modules[name] = type(sys)(name) | |
return sys.modules[name] | |
def GeventSpawn(target, *args, **kwargs): | |
return thread.start_new_thread(target, args, kwargs) | |
def GeventSpawnLater(seconds, target, *args, **kwargs): | |
def wrap(*args, **kwargs): | |
import time | |
time.sleep(seconds) | |
return target(*args, **kwargs) | |
return thread.start_new_thread(wrap, args, kwargs) | |
class GeventServerStreamServer(SocketServer.ThreadingTCPServer): | |
allow_reuse_address = True | |
def finish_request(self, request, client_address): | |
self.RequestHandlerClass(request, client_address) | |
class GeventPoolPool(object): | |
def __init__(self, size): | |
self._lock = threading.Semaphore(size) | |
def __target_wrapper(self, target, args, kwargs): | |
t = threading.Thread(target=target, args=args, kwargs=kwargs) | |
try: | |
t.start() | |
t.join() | |
except Exception as e: | |
logging.error('threading.Thread target=%r error:%s', target, e) | |
finally: | |
self._lock.release() | |
def spawn(self, target, *args, **kwargs): | |
self._lock.acquire() | |
return thread.start_new_thread(self.__target_wrapper, (target, args, kwargs)) | |
gevent = GeventImport('gevent') | |
gevent.queue = GeventImport('gevent.queue') | |
gevent.coros = GeventImport('gevent.coros') | |
gevent.server = GeventImport('gevent.server') | |
gevent.pool = GeventImport('gevent.pool') | |
gevent.queue.Queue = Queue.Queue | |
gevent.coros.Semaphore = threading.Semaphore | |
gevent.spawn = GeventSpawn | |
gevent.spawn_later = GeventSpawnLater | |
gevent.server.StreamServer = GeventServerStreamServer | |
gevent.pool.Pool = GeventPoolPool | |
del GeventImport, GeventSpawn, GeventSpawnLater, GeventServerStreamServer, GeventPoolPool | |
def echo_handler(sock, address): | |
rfile = sock.makefile('rb', -1) | |
wfile = sock.makefile('wb', 0) | |
while 1: | |
wfile.write(rfile.readline()) | |
def main(): | |
server = gevent.server.StreamServer(('', 8007), echo_handler) | |
server.serve_forever() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment