sendmessage not sending data to client immediately

#1

I like to use Atuobahn websocket library for my client / server apps development. My client will send a command to the server and wait for progress messages from the server and display them at real time. For my experiment, I have modified the echo server example to download a large file from the web and send progress messages at real time back to the client. I ran into some problem with the sendmessage on the server.py. I notice each call to self.sendMessage to send the progress status to the client doesn’t show up right away on the client side. From what I can tell it buffers up all the messages and send them all at once after the file is downloaded. Here is the server.py code:

import sys

import urllib2

from twisted.internet import reactor

from twisted.python import log

from twisted.web.server import Site

from twisted.web.static import File

from autobahn.websocket import WebSocketServerFactory, \

                           WebSocketServerProtocol, \
                           listenWS

class EchoServerProtocol(WebSocketServerProtocol):

def onMessage(self, msg, binary):
    # testing - just downlod a file
    self.downloadUrl()
def downloadUrl (self):
    url = "http://mirror.ox.ac.uk/sites/releases.ubuntu.com/releases//precise/ubuntu-12.04.3-desktop-i386.iso"
    tmpfile = '/tmp/' + url.split('/')[-1]
    u = urllib2.urlopen(url)
    f = open(tmpfile, 'wb')
    meta = u.info()
    fileSize = int(meta.getheaders("Content-Length")[0])
    self.sendMessage("Download: %s Bytes: %s\n" % (tmpfile, fileSize))
    print "Download: %s Bytes: %s\n" % (tmpfile, fileSize)
    # Start the download and report status to the client
    fileSizeDl = 0 
    blockSize = 8192
    while True:
        buffer = u.read(blockSize)
        if not buffer:
            break
        fileSizeDl += len(buffer)
        f.write(buffer)
        status = r"%10d [%3.2f%%]" % (fileSizeDl, fileSizeDl * 100. / fileSize)
        status = status + chr(8)*(len(status)+1) + '\n'
        # Send progress status to client
        self.sendMessage (status)
        print "%s" % (status)
    f.close()

if name == ‘main’:

if len(sys.argv) > 1 and sys.argv[1] == ‘debug’:

  log.startLogging(sys.stdout)
  debug = True

else:

  debug = False

factory = WebSocketServerFactory(“ws://localhost:9000”,

                                debug = debug,
                                debugCodePaths = debug)

factory.protocol = EchoServerProtocol

factory.setProtocolOptions(allowHixie76 = True)

listenWS(factory)

webdir = File(".")

web = Site(webdir)

reactor.listenTCP(8080, web)

reactor.run()

So do I have to make some call to flush the message to the socket immediately or is there some configuration call that I have to set?

Btw, I have try it with the broadcast example server and ran into the same issue. Any help would be greatly appreciated.

0 Likes

#2

at once after the file is downloaded. Here is the server.py code:

The code does a lot of blocking calls into the Python standard library:

urllib2.urlopen
f.write

and hence will block the Twisted reactor, and this will prevent timely asynchronous event processing.

This isn't Autobahn specific, but Twisted, and in general applies to all frameworks built around asynchrounous event dispatching and reactors.

You can read more in the Twisted docs, and or on SO, e.g.

http://stackoverflow.com/questions/19250458/send-json-with-autobahn-python/19252009#19252009

So do I have to make some call to flush the message to the socket
immediately or is there some configuration call that I have to set?

There is no such thing and it's not needed.

Autobahn is able to achieve event dispatching latencies <200 microsec.

If you need something faster (read in the range <10 microsec, you probably need to look elsewhere -- I guess you will first have to look for a suitable OS/kernel then)

Btw, I have try it with the broadcast example server and ran into the
same issue. Any help would be greatly appreciated.

Did you run the _unmodified_ example?

What did happen (turn on logging and report here)?

/Tobias

0 Likes