Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging.statistics doesn't work in Python 3 #10

Open
danielpcox opened this issue Sep 23, 2020 · 2 comments
Open

logging.statistics doesn't work in Python 3 #10

danielpcox opened this issue Sep 23, 2020 · 2 comments

Comments

@danielpcox
Copy link

I just set logging.statistics['WSGIserver whateverid']['Enabled'] = True and it crashed, telling me BufferReader doesn't have an attribute bytes_read, which is true. It looks like the problem stems from HTTPConnection's use of CP_makefile_PY3 in Python 3 which returns io.BufferedReader, and CP_makefile_PY2 in Python 2, which returns a custom thing that has a bytes_read attribute.

@danielpcox
Copy link
Author

danielpcox commented Sep 23, 2020

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
####################################################################################

@danielpcox
Copy link
Author

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant