From c8b4ba2b5b3e4dedec22e04203fe511f117f05cd Mon Sep 17 00:00:00 2001 From: "roberto@natty32" Date: Wed, 16 Mar 2011 20:54:54 +0100 Subject: [PATCH] updated iobound_async example and improved pep3333-input --- plugins/python/wsgi_handlers.c | 42 +++++++++++++--------------- tests/iobound_async.py | 51 ++++++++++++---------------------- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index 0f70eafa..bfe29ced 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -17,9 +17,8 @@ PyObject *uwsgi_Input_iter(PyObject * self) { PyObject *uwsgi_Input_next(PyObject * self) { - PyErr_SetNone(PyExc_StopIteration); + return PyErr_Format(PyExc_NotImplementedError, "wsgi.input __iter__() is not implemented"); - return NULL; } static void uwsgi_Input_free(uwsgi_Input *self) { @@ -29,7 +28,7 @@ static void uwsgi_Input_free(uwsgi_Input *self) { static PyObject *uwsgi_Input_read(uwsgi_Input *self, PyObject *args) { long len = 0; - size_t remains, chunk_size; + size_t remains; ssize_t rlen; char *tmp_buf; int fd; @@ -75,27 +74,22 @@ static PyObject *uwsgi_Input_read(uwsgi_Input *self, PyObject *args) { return res; } - chunk_size = remains; tmp_buf = uwsgi_malloc(remains); - while(remains) { - if (uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]) <= 0) { - free(tmp_buf); - return PyErr_Format(PyExc_ValueError, "error waiting for wsgi.input data"); - } - rlen = read(fd, tmp_buf + self->pos, remains); - if (rlen < 0) { - free(tmp_buf); - return PyErr_Format(PyExc_ValueError, "error reading wsgi.input data"); - } - - if (!rlen) break; - - self->pos += rlen; - remains -= rlen; + if (uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]) <= 0) { + free(tmp_buf); + return PyErr_Format(PyExc_IOError, "error waiting for wsgi.input data"); } - res = PyString_FromStringAndSize(tmp_buf, chunk_size); + rlen = read(fd, tmp_buf, remains); + if (rlen < 0) { + free(tmp_buf); + return PyErr_Format(PyExc_IOError, "error reading wsgi.input data"); + } + + self->pos += rlen; + res = PyString_FromStringAndSize(tmp_buf, rlen); + free(tmp_buf); return res; @@ -103,17 +97,19 @@ static PyObject *uwsgi_Input_read(uwsgi_Input *self, PyObject *args) { static PyObject *uwsgi_Input_readline(uwsgi_Input *self, PyObject *args) { - return NULL; + return PyErr_Format(PyExc_NotImplementedError, "wsgi.input readline() is not implemented"); + } static PyObject *uwsgi_Input_readlines(uwsgi_Input *self, PyObject *args) { - return NULL; + return PyErr_Format(PyExc_NotImplementedError, "wsgi.input readlines() is not implemented"); } static PyObject *uwsgi_Input_close(uwsgi_Input *self, PyObject *args) { - return NULL; + Py_INCREF(Py_None); + return Py_None; } static PyMethodDef uwsgi_Input_methods[] = { diff --git a/tests/iobound_async.py b/tests/iobound_async.py index 595c7881..be6062de 100644 --- a/tests/iobound_async.py +++ b/tests/iobound_async.py @@ -1,60 +1,43 @@ -import socket -import select -import errno import uwsgi def send_request(env, client): - client.setblocking(1) + uwsgi.send(client, b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n") # test for suspend/resume uwsgi.suspend() - yield env['x-wsgiorg.fdevent.writable'](client.fileno(), 2) - if env['x-wsgiorg.fdevent.timeout']: - return - - client.send(b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n") - - yield env['x-wsgiorg.fdevent.writable'](client.fileno(), 2) - if env['x-wsgiorg.fdevent.timeout']: - return - client.send(b"Host: www.google.it\r\n\r\n") + uwsgi.send(client, b"Host: www.google.it\r\n\r\n") while 1: - yield env['x-wsgiorg.fdevent.readable'](client.fileno(), 2) + yield uwsgi.wait_fd_read(client, 2) if env['x-wsgiorg.fdevent.timeout']: return - buf = client.recv(4096) - if len(buf) == 0: - break - else: + buf = uwsgi.recv(client, 4096) + if buf: yield buf + else: + break def application(env, start_response): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.setblocking(0) + c = uwsgi.async_connect('74.125.232.115:80') - #env['x-wsgiorg.fdevent.readable'] = lambda fd,t: "" - #env['x-wsgiorg.fdevent.writable'] = lambda fd,t: "" + # wait for connection + yield uwsgi.wait_fd_write(c, 2) + + if env['x-wsgiorg.fdevent.timeout']: + uwsgi.close(c) + raise StopIteration - #yield "" - - #c = s.connect_ex(('www.google.it', 80)) - c = s.connect_ex(('74.125.232.115', 80)) - if c == errno.EINPROGRESS: - yield env['x-wsgiorg.fdevent.writable'](s.fileno(), 2) - for r in send_request(env, s): - yield r - elif c == errno.EISCONN: - for r in send_request(env, s): + if uwsgi.is_connected(c): + for r in send_request(env, c): yield r else: start_response( '500 Internal Server Error', [ ('Content-Type', 'text/html')]) yield "Internal Server Error" - s.close() + uwsgi.close(c)