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

on_url not called intermittently? #20

Open
yohanboniface opened this issue Jun 27, 2017 · 1 comment
Open

on_url not called intermittently? #20

yohanboniface opened this issue Jun 27, 2017 · 1 comment

Comments

@yohanboniface
Copy link
Contributor

From time to time, my server output an error that let me think that Request.on_url is not called for the current stream.

Here is a small script to reproduce the behaviour:

import asyncio

from httptools import HttpRequestParser


class Request:

    def __init__(self):
        self.EOF = False

    def on_url(self, url: bytes):
        self.on_url_called = True

    def on_message_complete(self):
        self.EOF = True


async def serve(reader, writer):
    chunks = 2 ** 16
    req = Request()
    parser = HttpRequestParser(req)
    while True:
        data = await reader.read(chunks)
        parser.feed_data(data)
        if not data or req.EOF:
            break
    assert req.on_url_called
    writer.write(b'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK')
    writer.write_eof()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    coro = loop.create_task(asyncio.start_server(serve, '127.0.0.1', 8080))
    server = loop.run_until_complete(coro)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        print('Bye.')
    finally:
        server.close()
        loop.run_until_complete(server.wait_closed())
        loop.close()

To be run with python file.py

And then benchmark it with something like ab -n 10000 -c 8 'http://127.0.0.1:8080/'

Once every, say, 10000 requests, I get this error:

Task exception was never retrieved
future: <Task finished coro=<serve() done, defined at test_httptools.py:18> exception=AttributeError("'Request' object has no attribute 'on_url_called'",)>
Traceback (most recent call last):
  File "test_httptools.py", line 27, in serve
    assert req.on_url_called
AttributeError: 'Request' object has no attribute 'on_url_called'

Any hint? :)

Thanks!

@Kilerd
Copy link

Kilerd commented Jan 25, 2018

try to define on_url_called in initial function?

class Request:

    def __init__(self):
        self.EOF = False
        self.on_url_called = False

    def on_url(self, url: bytes):
        self.on_url_called = True

    def on_message_complete(self):
        self.EOF = True

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

2 participants