def delegateRendering(graphType, graphOptions, headers=None): if headers is None: headers = {} start = time() postData = graphType + '\n' + pickle.dumps(graphOptions) servers = settings.RENDERING_HOSTS[:] #make a copy so we can shuffle it safely shuffle(servers) connector_class = connector_class_selector(settings.INTRACLUSTER_HTTPS) 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 = connector_class(server) connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT # Send the request try: connection.request('POST', '/render/local/', postData, headers) except CannotSendRequest: connection = connector_class(server) #retry once connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT connection.request('POST', '/render/local/', postData, headers) # Read the response try: # Python 2.7+, use buffering of HTTP responses response = connection.getresponse(buffering=True) except TypeError: # Python 2.6 and older 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
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) connector_class = connector_class_selector(settings.INTRACLUSTER_HTTPS) 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 = connector_class(server) connection.timeout = settings.REMOTE_RENDER_CONNECT_TIMEOUT # Send the request try: connection.request('POST', '/render/local/', postData) except CannotSendRequest: connection = connector_class(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