1 import sys, os, Queue, atexit
3 dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
4 dir = os.path.join(dir, 'pypeers')
5 if dir not in sys.path:
9 from greensock import *
13 def _read_from_kernel(handler):
15 msg = read(handler.fd, handler.MAX_READ)
17 print >> sys.stderr, "out-kernel connexion closed"
19 autogreenlet(handler.handle_message, msg)
21 def add_handler(handler):
22 autogreenlet(_read_from_kernel, handler)
23 atexit.register(handler.close)
25 # ____________________________________________________________
31 #print 'thread runner %d waiting' % n
32 operation, answer = THREAD_QUEUE.get()
33 #print 'thread_runner %d: %r' % (n, operation)
35 res = True, operation()
37 res = False, sys.exc_info()
38 #print 'thread_runner %d: got %d bytes' % (n, len(res or ''))
42 def start_bkgnd_thread():
43 global THREAD_QUEUE, THREAD_LOCK
45 threadchannel.startup()
46 THREAD_LOCK = thread.allocate_lock()
47 THREAD_QUEUE = Queue.Queue()
49 thread.start_new_thread(thread_runner, (i,))
51 def wget(*args, **kwds):
55 kwds['unlock'] = THREAD_LOCK
58 return wget(*args, **kwds)
62 if THREAD_QUEUE is None:
64 answer = threadchannel.ThreadChannel()
65 THREAD_QUEUE.put((operation, answer))
66 ok, res = answer.receive()
70 #print 'wget returns %d bytes' % (len(res or ''),)