Beispiel #1
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     sub_task = asyncio.ensure_future(
         run_in_greenlet(this, future, _sample), )
     this.parent.switch(sub_task)
     self.assertRaises(KeyError, future.result)
Beispiel #2
0
        def websocket_view(context, request):
            uwsgi.websocket_handshake()
            this = greenlet.getcurrent()
            this.has_message = False
            q_in = asyncio.Queue()
            q_out = asyncio.Queue()

            # make socket proxy
            if inspect.isclass(view):
                view_callable = view(context, request)
            else:
                view_callable = view
            ws = UWSGIWebsocket(this, q_in, q_out)

            # start monitoring websocket events
            asyncio.get_event_loop().add_reader(uwsgi.connection_fd(),
                                                uwsgi_recv_msg, this)

            # NOTE: don't use synchronize because we aren't waiting
            # for this future, instead we are using the reader to return
            # to the child greenlet.

            future = asyncio.Future()
            asyncio. async (run_in_greenlet(this, future, view_callable, ws))

            # switch to open
            this.parent.switch()

            while True:
                if future.done():
                    if future.exception() is not None:
                        raise WebsocketClosed from future.exception()
                    raise WebsocketClosed

                # message in
                if this.has_message:
                    this.has_message = False
                    try:
                        msg = uwsgi.websocket_recv_nb()
                    except OSError:
                        msg = None

                    if UWSGIWebsocketMapper.use_str:
                        with suppress(Exception):
                            print('howdy')
                            msg = bytes.decode(msg)

                    if msg or msg is None:
                        q_in.put_nowait(msg)

                # message out
                if not q_out.empty():
                    msg = q_out.get_nowait()
                    try:
                        uwsgi.websocket_send(msg)
                    except OSError:
                        q_in.put_nowait(None)

                this.parent.switch()
Beispiel #3
0
        def websocket_view(context, request):
            uwsgi.websocket_handshake()
            this = greenlet.getcurrent()
            this.has_message = False
            q_in = asyncio.Queue()
            q_out = asyncio.Queue()

            # make socket proxy
            if inspect.isclass(view):
                view_callable = view(context, request)
            else:
                view_callable = view
            ws = UWSGIWebsocket(this, q_in, q_out)

            # start monitoring websocket events
            asyncio.get_event_loop().add_reader(
                uwsgi.connection_fd(),
                uwsgi_recv_msg,
                this
            )

            # NOTE: don't use synchronize because we aren't waiting
            # for this future, instead we are using the reader to return
            # to the child greenlet.

            future = asyncio.Future()
            asyncio.async(
                run_in_greenlet(this, future, view_callable, ws)
            )

            # switch to open
            this.parent.switch()

            while True:
                if future.done():
                    if future.exception() is not None:
                        raise future.exception()
                    raise WebsocketClosed

                # message in
                if this.has_message:
                    this.has_message = False
                    try:
                        msg = uwsgi.websocket_recv_nb()
                    except OSError:
                        msg = None

                    if msg or msg is None:
                        q_in.put_nowait(msg)

                # message out
                if not q_out.empty():
                    msg = q_out.get_nowait()
                    try:
                        uwsgi.websocket_send(msg)
                    except OSError:
                        q_in.put_nowait(None)

                this.parent.switch()
Beispiel #4
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     sub_task = asyncio.async(
         run_in_greenlet(this, future, _sample),
     )
     this.parent.switch(sub_task)
     self.assertRaises(KeyError, future.result)
Beispiel #5
0
 def async_tween(request):
     this = greenlet.getcurrent()
     future = asyncio.Future()
     sub_task = asyncio.ensure_future(
         run_in_greenlet(this, future, _async_action), )
     self.assertIsInstance(sub_task, asyncio.Future)
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), 12)
     return future
Beispiel #6
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     message = 12
     sub_task = asyncio.ensure_future(
         run_in_greenlet(this, future, _sample, message), )
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), message)
     return message + 1
Beispiel #7
0
 def async_tween(request):
     this = greenlet.getcurrent()
     future = asyncio.Future()
     sub_task = asyncio.ensure_future(
         run_in_greenlet(this, future, _async_action),
     )
     self.assertIsInstance(sub_task, asyncio.Future)
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), 12)
     return future
Beispiel #8
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     message = 12
     sub_task = asyncio.async(
         run_in_greenlet(this, future, _sample, message),
     )
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), message)
     return message + 1
Beispiel #9
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     message = 12
     sub_task = asyncio.async(run_in_greenlet(
         this,
         future,
         _chain,
         message,
     ))
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), message - 1)
     return message + 1
Beispiel #10
0
 def _greenlet():
     this = greenlet.getcurrent()
     future = asyncio.Future()
     message = 12
     sub_task = asyncio.ensure_future(run_in_greenlet(
         this,
         future,
         _chain,
         message,
     ))
     this.parent.switch(sub_task)
     self.assertEqual(future.result(), message - 1)
     return message + 1