Sending multiple `HTTP` requests `ON` `GREEN` thread
174126
def hog(concurrency, requests, limit, timeout,
        params, paramfile, headers, headerfile, method, url):
    ''''''

    params = parse_from_list_and_file(params, paramfile)
    headers = parse_from_list_and_file(headers, headerfile)

    # Running information
    click.echo(HR)
    click.echo("Hog is running with {} threads, ".format(concurrency) +
          "{} requests ".format(requests) +
          "and timeout in {} second(s).".format(timeout))
    if limit != 0:
        click.echo(">>> Limit: {} request(s) per second.".format(limit))
    click.echo(HR)

    # Let's begin!
    result = Hog(callback).run(url, params, headers, method, timeout, concurrency, requests, limit)

    sys.stdout.write("\n")
    print_result(result)
418597
def communicate(self):
        """"""
        request_seen = False
        try:
            while True:
                # (re)set req to None so that if something goes wrong in
                # the RequestHandlerClass constructor, the error doesn't
                # get written to the previous request.
                req = None
                req = self.RequestHandlerClass(self.server, self)
                
                # This order of operations should guarantee correct pipelining.
                req.parse_request()
                if not req.ready:
                    # Something went wrong in the parsing (and the server has
                    # probably already made a simple_response). Return and
                    # let the conn close.
                    return
                
                request_seen = True
                req.respond()
                if req.close_connection:
                    return
        except socket.error, e:
            errnum = e.args[0]
            # sadly SSL sockets return a different (longer) time out string
            if errnum == 'timed out' or errnum == 'The read operation timed out':
                # Don't error if we're between requests; only error
                # if 1) no request has been started at all, or 2) we're
                # in the middle of a request.
                # See http://www.cherrypy.org/ticket/853
                if (not request_seen) or (req and req.started_request):
                    # Don't bother writing the 408 if the response
                    # has already started being written.
                    if req and not req.sent_headers:
                        try:
                            req.simple_response("408 Request Timeout")
                        except FatalSSLAlert:
                            # Close the connection.
                            return
            elif errnum not in socket_errors_to_ignore:
                if req and not req.sent_headers:
                    try:
                        req.simple_response("500 Internal Server Error",
                                            format_exc())
                    except FatalSSLAlert:
                        # Close the connection.
                        return
            return
        except (KeyboardInterrupt, SystemExit):
            raise
        except FatalSSLAlert:
            # Close the connection.
            return
        except NoSSLError:
            if req and not req.sent_headers:
                # Unwrap our wfile
                self.wfile = CP_fileobject(self.socket._sock, "wb", self.wbufsize)
                req.simple_response("400 Bad Request",
                    "The client sent a plain HTTP request, but "
                    "this server only speaks HTTPS on this port.")
                self.linger = True
        except Exception:
            if req and not req.sent_headers:
                try:
                    req.simple_response("500 Internal Server Error", format_exc())
                except FatalSSLAlert:
                    # Close the connection.
                    return
540197
function send(httpMethod, path, req) {
    req = req || {};
    httpMethod = httpMethod.toUpperCase();

    var deferred = Q.defer();
    var url = baseUrl + path;
    var data = req.data || {};          // separate out data from request
    delete req.data;

    var isAdmin = req.caller && req.caller.role === 'admin';
    delete req.caller;

    var id = req._id || data._id;       // get id from request or data
    if (req._id) { delete req._id; }    // delete id from request so no dupe

    var session = cls.getNamespace('appSession');
    var caller = session && session.active && session.get('caller');
    if (caller && !isAdmin) {
        _.extend(req, {                 // onBehalfOf used by API to know who is the caller
            onBehalfOfType: caller.type,
            onBehalfOfId:   caller._id + '',
            onBehalfOfRole: caller.role,
            onBehalfOfName: caller.name
        });
    }

    _.each(req, function (val, key) {
        if (!_.isString(val)) { req[key] = JSON.stringify(val); }
    });

    // replace the ID portion of the URL
    url = id ?
        url.replace('{_id}', id + '') :
        url.replace('/{_id}', '');

    var reqConfig = {
        headers:    headers,
        url:        url,
        method:     httpMethod,
        qs:         _.isEmpty(req) ? undefined : req,
        json:       _.isEmpty(data) ? true : data
    };

    // if baseUrl not there yet, wait for 100 seconds until it is (i.e. during system startup)
    if (!baseUrl) {
        setTimeout(function () {
            makeRequest(reqConfig, deferred);
        }, 100);
    }
    else {
        makeRequest(reqConfig, deferred);
    }

    return deferred.promise;
}
1033517
def sendResult(self, future):
        """"""
        # Greenlets cannot be pickled
        future.greenlet = None
        assert future._ended(), "The results are not valid"
        self.socket.sendResult(future)
1246288
function Request (method, args, attempts, proxyCondition) {
    // constraint
    attempts = attempts || 0;
    if (attempts > 10)
        throw `Maximum number of request attempts exceeded (10 total) for "${args.url}"`;
    // workflow
    if (!args.url) throw 'URL undefined in smappi-cl/lib/request';
    args.data = args.data || {}; // for POST/PUT
    args.params = args.params || {}; // for GET
    args.headers = args.headers || {};
    args.options = args.options || {};
    if (!args.options.hasOwnProperty('followLocation')) {
        args.options.followLocation = true;
    }
    if (args.options.proxy == false) {
        args.proxy = null;
    } else {
        if (proxyCondition && !proxy.isActive()) {
            // if not disable proxy (for internal request, see proxy.js)
            // and there proxyCondition
            // and not active proxy
            proxy.next(); // then activate proxy if used withProxy!
        }
        args.proxy = proxy.currentHref();
    }
    args.method = method;
    if (!args.options.async) { // Synchronous
        var contentFile = ".smappi-request-content-" + process.pid;
        var syncFile = ".smappi-request-sync-" + process.pid;
        fs.writeFileSync(syncFile, "", "utf8");
        // The async request the other Node process executes
        // Start the other Node Process, executing this string
        // console.log(process.argv[0], __dirname + '/worker.js', process.pid, "'" + JSON.stringify(args) + "'");
        var syncProc = spawn(process.argv[0], [
            __dirname + '/worker.js',
            process.pid,
            JSON.stringify(args)
        ]);
        while (fs.existsSync(syncFile)) {
            // Wait while the sync file is empty
        }
        var resp = JSON.parse(fs.readFileSync(contentFile, 'utf8'));
        // Kill the child process once the file has data
        syncProc.stdin.end();
        // Remove the temporary file
        fs.unlinkSync(contentFile);
        let retry = () => {
            // if use proxy and request is broken, then turn next
            if (args.options.proxy !== false) {
                proxy.skip(); // skip current
                proxy.next();
            }
            return new Request(method, args, attempts + 1, proxyCondition);
        }
        if (resp.err) {
            if (Object.keys(resp.err).length) {
                let errStr = JSON.stringify(resp.err);
                if (resp.err.options && resp.err.options.command == 'connect') {
                    let postScriptum = '';
                    if (resp.err.options.proxy)
                        postScriptum = ' (probably proxy is wrong)';
                    logger.error(`Problem while connect for "${args.url}"${postScriptum}. Request will be retry. ERR: ${errStr}`);
                    return retry();
                } else if (resp.err.code == 'ECONNRESET') {
                    logger.error(`Connection problems for "${args.url}". Request will be retry. ERR: ${errStr}`);
                    return retry();
                } else if (resp.err.code == 'ETIMEOUT' || resp.err.code == 'ETIMEDOUT') {
                    logger.error(`Connection timeout exceeded for "${args.url}". Request will be retry. ERR: ${errStr}`);
                    return retry();
                } else if (resp.err.code == 'ENOTFOUND' && resp.err.syscall == 'getaddrinfo') {
                    let msg = `Host "${resp.err.hostname}" not found. Please verify A-record for this domain.`;
                    logger.error(msg);
                    throw msg;
                } else {
                    throw errStr;
                }
            } else if (args.proxy && resp.ctx == 'request') {
                return retry(); // Otherwise retry again
            } else if (resp.message) {
                throw resp.message;
            } else {
                throw 'Problem with connection in smappi-cl/lib/request';
            }
        } else {
            if ([301, 302].indexOf(resp.status) != -1) {
                if (args.options.followLocation) {
                    let location = url.parse(resp.headers.location);
                    if (!location.protocol) {
                        let srcUrl = url.parse(args.url);
                        // add protocol for location without proto, example "//smappi.org/path/to" or relative path "/path/to"
                        location.protocol = srcUrl.protocol;
                        if (!location.href.startsWith('//')) { // relative, not shortcut proto
                            // relative path to current hostname, example "/path/to"
                            location.hostname = srcUrl.hostname;
                        }
                    }
                    args.url = location.format();
                    return new Request(method, args, attempts + 1, proxyCondition);
                }
            }
            let response = new Response(resp, args.url);
            // console.log('RequestResponse check condition', args.url, response.status, !proxyCondition || proxyCondition(response))
            if (!proxyCondition || proxyCondition(response)) {
                // If not proxyCondition or proxyCondition is success
                // return Response
                return response;
            } else {
                return retry(); // Otherwise retry again
            }
        }
    } else {
        throw 'Temporary! Not Implemented Smappi CL Async Requests';
    }
}
1199228
def green_callback(fn, obj=None, green_mode=None):
    """"""
    executor = get_object_executor(obj, green_mode)

    @wraps(fn)
    def greener(*args, **kwargs):
        return executor.submit(fn, *args, **kwargs)

    return greener
1306021
def on_threads_request(self, py_db, request):
        '''
        
        '''
        return self.api.list_threads(py_db, request.seq)
474344
public void sendAll(String apiKey, Map<String, ? extends Collection<?>> messagesByQueue) {
        checkNotNull(messagesByQueue, "messagesByQueue");
        if (messagesByQueue.isEmpty()) {
            return;
        }
        if (messagesByQueue.size() == 1) {
            // Prefer the single queue REST call because it'll log the queue name in the HTTP server request logs.
            Map.Entry<String, ? extends Collection<?>> entry = Iterables.getOnlyElement(messagesByQueue.entrySet());
            sendAll(apiKey, entry.getKey(), entry.getValue());
            return;
        }
        try {
            URI uri = _queueService.clone()
                    .segment("_sendbatch")
                    .build();
            _client.resource(uri)
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey)
                    .post(messagesByQueue);
        } catch (EmoClientException e) {
            throw convertException(e);
        }
    }
306391
def request(uri, method, data = nil, type = :write, request_options = {})
      exceptions = []

      connect_timeout = @connect_timeout
      send_timeout = if type == :search
        @search_timeout
      elsif type == :batch
        type = :write
        @batch_timeout
      else
        @send_timeout
      end
      receive_timeout = type == :search ? @search_timeout : @receive_timeout

      thread_local_hosts(type != :write).each_with_index do |host, i|
        connect_timeout += 2 if i == 2
        send_timeout += 10 if i == 2
        receive_timeout += 10 if i == 2

        thread_index_key = type != :write ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
        Thread.current[thread_index_key] = host[:index]
        host[:last_call] = Time.now.to_i

        host[:session].connect_timeout = connect_timeout
        host[:session].send_timeout = send_timeout
        host[:session].receive_timeout = receive_timeout
        begin
          return perform_request(host[:session], host[:base_url] + uri, method, data, request_options)
        rescue AlgoliaProtocolError => e
          raise if e.code / 100 == 4
          exceptions << e
        rescue => e
          exceptions << e
        end
        host[:session].reset_all
      end
      raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
    end
1252766
synchronized private void startFrameThreads() {
    if(!process)
      return;
    int requests = unassignedEncodeRequests.size();
    int frames = inactiveFrameThreads.size();
    frames = (requests <= frames) ? requests:frames;
    for(int i = 0; i < frames; i++) {
      FrameThread ft = inactiveFrameThreads.remove(0);
      Thread thread = new Thread(ft);
      frameThreadMap.put(ft, thread);
      thread.start();
    }
  }
1091677
def multi_request(method, paths, body=nil, param=nil, header=nil)
      @multi = true

      # Response storage, if not already set
      if @responses.nil?
        @responses = []
      end

      # Handle one or many paths
      if paths.is_a? String or paths.is_a? URI
        paths = [paths]
      end

      # Handle (URI) objects
      paths = paths.map {|p| p.to_s }

      log.debug "Queueing multi-#{method} requests, concurrency: #{concurrency}, path(s): #{ paths.size == 1 ? paths[0]: paths.size }"

      paths.each do | path |

        multi_request = request(path, method.downcase.to_sym, body, param, header)
        multi_request.on_complete do | response |
          log.debug "Multi-#{method} [#{paths.size}]: "+log_message(response)
          @responses << response
        end
        hydra.queue(multi_request)
      end
      hydra
    end
131567
def handleRequest(self, obj, socket, multi=False):
        '''
        
        '''
        while 1:
            try:
                msg = socket.recv_multipart(zmq.NOBLOCK)
                method = msg[0].decode()
                try:
                    args = pickle.loads(msg[1], **pickle_options)
                except ValueError as err:
                    self._logger.notice('Request %s failed. Caught %s %s' %
                                        (method, type(err), str(err)))
                    self._sendAnswer(socket, err)
                self._logger.debug("received request %s %s" %
                                   (method, str(args)))
                try:
                    res= getattr(obj, method).__call__(*args)
                except Exception as e:
                    self._logger.notice('Request %s %s failed. Caught %s %s' %
                                        (method, str(args), type(e), str(e)))
                    res= e
                self._sendAnswer(socket, res)
            except zmq.ZMQError:
                return
            except Exception as e:
                self._logger.error("Unknown error %s" % str(e))
            if not multi:
                break
158488
def threadsafe_call(self, fn, *args, **kwargs):
        """"""
        def handler():
            try:
                fn(*args, **kwargs)
            except Exception:
                warn("error caught while excecuting async callback\n%s\n",
                     format_exc())

        def greenlet_wrapper():
            gr = greenlet.greenlet(handler)
            gr.switch()

        self._async_session.threadsafe_call(greenlet_wrapper)
34289
def handle_one_request(self):
        """Copy of WSGIRequestHandler, but with different ServerHandler"""

        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = ''
            self.request_version = ''
            self.command = ''
            self.send_error(414)
            return

        if not self.parse_request():  # An error code has been sent, just exit
            return

        handler = ServerHandler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ()
        )
        handler.request_handler = self      # backpointer for logging
        handler.run(self.server.get_app())
1211398
private void sendNextPayload() {
		singleThreadExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					sendNextPayloadSync();
				} catch (Exception e) {
					ApptentiveLog.e(e, "Exception while trying to send next payload");
					logException(e);
				}
			}
		});
	}
961287
def _worker_http(self, monitor):
        
        self.thread_debug("process_http", data=monitor, module='handler')
        query = monitor['query']
        method = query['method'].lower()
        self.stats.http_run += 1
        try:
            target = monitor['target']
            url = 'http://{host}:{port}{path}'.format(path=query['path'], **target)
            response = {
                'url': url,
                'status': 'failed',
                'result': {},
                'monitor': monitor,
                'message': 'did not meet expected result or no expected result defined',
                'elapsedms': monitor['timeout']*1000,
                'code':0
            }

            # not sed_env_dict -- we do not want to xref headers
            headers = query.get('headers', {})
            for elem in headers:
                headers[elem] = self.sed_env(headers[elem], {}, '')

            res = response['result'] = getattr(requests, method)(url,
                                                                 headers=headers,
                                                                 timeout=monitor['timeout'])
            response['code'] = res.status_code
            response['elapsedms'] = res.elapsed.total_seconds() * 1000
            if 'response-code' in monitor['expect']:
                if int(monitor['expect']['response-code']) == res.status_code:
                    response['message'] = ''
                    response['status'] = 'ok'
                else: # abort with failure, do not pass go
                    return response

            if 'content' in monitor['expect']:
                if monitor['expect']['content'] in res.text:
                    response['message'] = ''
                    response['status'] = 'ok'
                else: # abort with failure, do not pass go
                    return response

            if 'regex' in monitor['expect']:
                if re.search(monitor['expect']['regex'], res.text):
                    response['message'] = ''
                    response['status'] = 'ok'
                else: # abort with failure, do not pass go
                    return response

        except requests.exceptions.Timeout:
            response['message'] = 'timeout'
        except requests.exceptions.ConnectionError:
            response['message'] = 'connect-failed'
            response['elapsedms'] = -1
        return response
721128
def call_requests(
    requests: Union[Request, Iterable[Request]], methods: Methods, debug: bool
) -> Response:
    
    if isinstance(requests, collections.Iterable):
        return BatchResponse(safe_call(r, methods, debug=debug) for r in requests)
    return safe_call(requests, methods, debug=debug)