From: Eric S. Raymond Date: Mon, 1 Oct 2012 03:33:09 +0000 (-0400) Subject: Import Peter Scott's use of green threads. X-Git-Tag: 1.3~14 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8f0ff15f729fcec0a07f270d76d5cfef46262b7e;p=irker.git Import Peter Scott's use of green threads. --- diff --git a/irkerd b/irkerd index 041063e..4e2f9ba 100755 --- a/irkerd +++ b/irkerd @@ -39,6 +39,16 @@ CONNECTION_MAX = 200 # Avoid pushing per-process thread or fd limits # No user-serviceable parts below this line +# This black magic imports support for green threads (coroutines), +# then has kinky sex with the threading library internals. +# Threads then become ultra-light-weight and cooperatively +# scheduled. +try: + import eventlet; eventlet.monkey_patch() + green_threads = True +except ImportError: + green_threads = False + import sys, json, getopt, urlparse, time, random import threading, Queue, SocketServer import irc.client, logging @@ -394,5 +404,10 @@ if __name__ == '__main__': udpserver = SocketServer.UDPServer((HOST, PORT), IrkerUDPHandler) threading.Thread(target=tcpserver.serve_forever).start() threading.Thread(target=udpserver.serve_forever).start() + # Main thread has to stay alive forever for the cooperative + # scheduling of the green threads to work. + if green_threads: + while True: + time.sleep(10) # end