sendMessage being delayed

75 views
Skip to first unread message

Chris Meyer

unread,
Dec 5, 2014, 12:42:49 PM12/5/14
to autob...@googlegroups.com
I have the following server and client. I would expect the client to print the messages at one per second.

However, the client only prints when the send_my_messages() function is complete (after 5s). I also tried the threaded version (change True to False in onOpen) and then the client prints the first message immediately, but then delays the remaining 4 messages until the last has been sent.

Can someone help me understand I'm doing wrong?

Using Python 2.7.8 (anaconda), autobahn 0.9.3-3, twisted 14.0.2.

import sys
import time
import threading

from twisted.python import log
from twisted.internet import reactor

from autobahn.twisted.websocket import WebSocketServerProtocol
from autobahn.twisted.websocket import WebSocketServerFactory


class MyServerProtocol(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def send_my_messages(self):
        for t in range(5):
            self.sendMessage("time " + str(t))
            time.sleep(1.0)

    def onOpen(self):
        if True:
            self.send_my_messages()
        else:
            threading.Thread(target=self.send_my_messages).start()


if __name__ == '__main__':

    log.startLogging(sys.stdout)

    factory = WebSocketServerFactory("ws://localhost:9000")
    factory.protocol = MyServerProtocol

    reactor.listenTCP(9000, factory)
    reactor.run()

-------------------------------------------------------

<!DOCTYPE html>
<html>
   <head>
      <script type="text/javascript">
         window.onload = function() {
            var socket = new WebSocket("ws://127.0.0.1:9000");
            socket.onmessage = function(e) {
               console.log("Text message received: " + e.data);
            }
         };
      </script>
   </head>
   <body>
      <p>Open your browser's JavaScript console to see what's happening (hit F12).</p>
   </body>
</html>

Tobias Oberstein

unread,
Dec 6, 2014, 5:56:40 AM12/6/14
to autob...@googlegroups.com
Hi Chris,

Am 05.12.2014 18:42, schrieb Chris Meyer:
> I have the following server and client. I would expect the client to
> print the messages at one per second.
>
> However, the client only prints when the send_my_messages() function is
> complete (after 5s). I also tried the threaded version (change True to
> False in onOpen) and then the client prints the first message
> immediately, but then delays the remaining 4 messages until the last has
> been sent.
>
> Can someone help me understand I'm doing wrong?

a) You are blocking the reactor (time.sleep)
b) You are using threads (threads are evil)

Rgd a), check out:

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/websocket/broadcast/server.py#L61

Rgd b): you certainly don't need threads for what you seemingly want to
do. There are valid use cases for using threads even with Twisted, but
you should not what you are doing.

Cheers,
/Tobias
> --
> You received this message because you are subscribed to the Google
> Groups "Autobahn" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autobahnws+...@googlegroups.com
> <mailto:autobahnws+...@googlegroups.com>.
> To post to this group, send email to autob...@googlegroups.com
> <mailto:autob...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autobahnws/d92462d1-0b5e-416a-b03c-241bf83017f8%40googlegroups.com
> <https://groups.google.com/d/msgid/autobahnws/d92462d1-0b5e-416a-b03c-241bf83017f8%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages