Open danielpcox opened 4 years ago
Here's the monkeypatching I'm currently doing to work around this problem:
####################################################################################
## Monkey patches for wsgiserver to fix statistics collection
####################################################################################
import io
import socket
import wsgiserver
class CP_BufferedReader(io.BufferedReader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bytes_read = 0
def read(self, *args, **kwargs):
bs = super().read(*args, **kwargs)
self.bytes_read += len(bs)
return bs
def readline(self, *args, **kwargs):
bs = super().readline(*args, **kwargs)
self.bytes_read += len(bs)
return bs
class CP_BufferedWriter(wsgiserver.CP_BufferedWriter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bytes_written = 0
def write(self, b):
s = super().write(b)
self.bytes_written += s
return s
def CP_makefile_PY3(sock, mode='r', bufsize=io.DEFAULT_BUFFER_SIZE):
if 'r' in mode:
return CP_BufferedReader(socket.SocketIO(sock, mode), bufsize)
else:
return CP_BufferedWriter(socket.SocketIO(sock, mode), bufsize)
wsgiserver.CP_makefile = CP_makefile_PY3
####################################################################################
I also get an error when I first enable statistics, and behavior gets wonky:
Exception in thread CP Server Thread-9:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/root/.local/share/virtualenvs/app-4PlAip0Q/lib/python3.8/site-packages/wsgiserver.py", line 1579, in run
self.work_time += time.time() - self.start_time
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
I just set
logging.statistics['WSGIserver whateverid']['Enabled'] = True
and it crashed, telling me BufferReader doesn't have an attributebytes_read
, which is true. It looks like the problem stems fromHTTPConnection
's use ofCP_makefile_PY3
in Python 3 which returns io.BufferedReader, andCP_makefile_PY2
in Python 2, which returns a custom thing that has abytes_read
attribute.