开发者

Stream stdio to a website (like buildbot)

So I am trying to work in a stdio stream for a we开发者_如何转开发bapp similar to Buildbot. Does anyone know how Buildbot deals with stdio? It is streaming (so it seems) and that would be exactly what I need for this program.

Does anyone know how to do this?

Any help would be greatly appreciated.

I am using Python with Django by the way


Buildbot uses Twisted, which has a rather different programming style from Django.

I'd say you'd have to write a service in Twisted to do what you're after. Communicate with it from Django and have it do the streaming part you need.

(In Django, as most web apps, you have one thread/call per request that blocks on db/io calls. In Twisted, you defer async function calls to the library and have callbacks to continue once the io is done. It feel a bit wierd at first, but it's actually quite nice, especially for apps that aren't just web-servers.)


Django allows you to return iterators from views. You could return an iterator that incrementally reads the stream and yields it back to django. request-response/#passing-iterators ex. snippet

Here is the simplest possible solution

from django.core.servers.basehttp import FileWrapper
from subprocess import Popen, PIPE
from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser) #SECURE THIS SHIT
def stdout_cmd(request, command):
              #buffsize=1 reads a char at a time
    process = Popen(command, shell=True, buffsize='1', stdout=PIPE)
              # FileWrapper creates an iterator class for stdout
    wrapper = FileWrapper(process.stdout)
              # text/plain causes monospaced output in browser
    return HttpResponse(wrapper, content_type='text/plain')

As long as you can create a file-like object you can wrap it in FileWrapper. Be aware that if output of you shell is sporadic, you will want to create your own iterator that reads a line at a time e.g.

wrapper = (line for line in process.stdout) # brackets create an iterator object

NOTE: This will consume worker threads and block people connecting to your server if you have too many concurrent long-poll pages loading. considder using gevent/greenlets. for high-traffic environments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜