Esempio n. 1
0
def delegateRendering(graphType, graphOptions):
    start = time()
    postData = graphType + '\n' + pickle.dumps(graphOptions)
    servers = settings.RENDERING_HOSTS[:]  #make a copy so we can shuffle it safely
    shuffle(servers)
    for server in servers:
        start2 = time()
        try:
            # Get a connection
            try:
                pool = connectionPools[server]
            except KeyError:  #happens the first time
                pool = connectionPools[server] = set()
            try:
                connection = pool.pop()
            except KeyError:  #No available connections, have to make a new one
                connection = HTTPConnectionWithTimeout(server)
                connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT
            # Send the request
            try:
                connection.request('POST', '/render/local/', postData)
            except CannotSendRequest:
                connection = HTTPConnectionWithTimeout(server)  #retry once
                connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT
                connection.request('POST', '/render/local/', postData)
            # Read the response
            response = connection.getresponse()
            assert response.status == 200, "Bad response code %d from %s" % (
                response.status, server)
            contentType = response.getheader('Content-Type')
            imageData = response.read()
            assert contentType == 'image/png', "Bad content type: \"%s\" from %s" % (
                contentType, server)
            assert imageData, "Received empty response from %s" % server
            # Wrap things up
            log.rendering('Remotely rendered image on %s in %.6f seconds' %
                          (server, time() - start2))
            log.rendering(
                'Spent a total of %.6f seconds doing remote rendering work' %
                (time() - start))
            pool.add(connection)
            return imageData
        except:
            log.exception(
                "Exception while attempting remote rendering request on %s" %
                server)
            log.rendering(
                'Exception while remotely rendering on %s wasted %.6f' %
                (server, time() - start2))
            continue
Esempio n. 2
0
def renderMyGraphView(request, username, graphName):
    profile = getProfileByUsername(username)
    assert profile, "No such user '%s'" % username
    try:
        graph = profile.mygraph_set.get(name=graphName)
    except ObjectDoesNotExist:
        assert False, "User %s doesn't have a MyGraph named '%s'" % (username,
                                                                     graphName)
    (proto, host, path, query, frag) = urlsplit(graph.url)
    if query: path += '?' + query
    conn = HTTPConnectionWithTimeout(host)
    conn.request('GET', path)
    resp = conn.getresponse()
    assert resp.status == 200, "Failed to retrieve image from URL %s" % graph.url
    imageData = resp.read()
    return buildResponse(imageData)