Fix wsproxy CPU usage without affecting latency.

Instead of selecting on everything every time, only select the writers
that we have items queued for. Most of the time the writers are ready
so if we select on them even when we don't have anything to send we
will fall into a tight loop.
This commit is contained in:
Joel Martin 2010-05-11 09:25:29 -05:00
parent 4ce2696d52
commit 92f572a249
1 changed files with 8 additions and 7 deletions

View File

@ -9,7 +9,7 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
'''
import sys, socket, ssl, time
import sys, socket, ssl
from select import select
from websocket import *
@ -32,15 +32,16 @@ def do_proxy(client, target):
cqueue = []
cpartial = ""
tqueue = []
socks = [client, target]
rlist = [client, target]
while True:
time.sleep(0.01) # 10ms
ins, outs, excepts = select(socks, socks, socks, 1)
wlist = []
if tqueue: wlist.append(target)
if cqueue: wlist.append(client)
ins, outs, excepts = select(rlist, wlist, [], 1)
if excepts: raise Exception("Socket exception")
if tqueue and target in outs:
if target in outs:
dat = tqueue.pop(0)
sent = target.send(dat)
if sent == len(dat):
@ -50,7 +51,7 @@ def do_proxy(client, target):
traffic(".>")
##log.write("Target send: %s\n" % map(ord, dat))
if cqueue and client in outs:
if client in outs:
dat = cqueue.pop(0)
sent = client.send(dat)
if sent == len(dat):