多线程进行http请求
昨天需要一个线下脚本进行单播推送,大约有1kw个用户,考虑到推送速度就临时搞了个请求线上的一个脚本
/**
* 临时支持invoke单播推送
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "ghttp.h" #include "log.h" //调试模式
#define DEBUGS
//最大线程数
#define MAX_THREADS 30 #ifndef DEBUGS
char *global_uri = "xxx";
#else
char *global_uri = "xxx";
#endif struct active_t{
pthread_mutex_t active_mutex;
pthread_cond_t active_cond;
int active;
}active_struct; const char *global_request_conf = "conf/request.conf"; void *work_fun(void *arg); static void _init_main(){
open_log();
print_log(DEBUG, "%s", "start...");
} static void _shut_down(){
print_log(DEBUG, "%s", "end...");
close_log();
} static void error_die(const char *msg){
perror(msg);
exit();
} /**
* 请求mapi
*/
int mapi_push(char *data, char *ret, int ret_len){
ghttp_request *request = NULL;
request = ghttp_request_new();
ghttp_set_uri(request, global_uri);
ghttp_set_type(request, ghttp_type_post);
ghttp_set_header(request, http_hdr_Connection, "close");
ghttp_set_header(request, http_hdr_Content_Type, "application/x-www-form-urlencoded");
ghttp_set_body(request, data, strlen(data));
ghttp_prepare(request);
int status = ghttp_process(request);
if(status == ghttp_error){
return -;
}
memset(ret, , ret_len);
strncpy(ret, ghttp_get_body(request), ghttp_get_body_len(request));
ret[strlen(ret)] = '\0';
int http_code = ghttp_status_code(request);
ghttp_request_destroy(request);
return http_code;
} int main(){
_init_main(); FILE *fp = fopen(global_request_conf, "r");
if(!fp){
error_die("fopen error");
}
char buf[];
memset(buf, , );
pthread_t thid;
while(!feof(fp) && fgets(buf, , fp) != NULL){
//max thread
pthread_mutex_lock(&active_struct.active_mutex);
while(active_struct.active >= MAX_THREADS){
pthread_cond_wait(&active_struct.active_cond, &active_struct.active_mutex);
}
pthread_mutex_unlock(&active_struct.active_mutex);
//run
pthread_create(&thid, NULL, work_fun, (void *)buf);
if(!thid){
printf("create thread error");
continue;
}
/*active+1*/
pthread_mutex_lock(&active_struct.active_mutex);
active_struct.active++;
pthread_mutex_unlock(&active_struct.active_mutex);
}
//wait for all thread done
pthread_mutex_lock(&active_struct.active_mutex);
while(active_struct.active != ){
pthread_cond_wait(&active_struct.active_cond, &active_struct.active_mutex);
}
pthread_mutex_unlock(&active_struct.active_mutex);
//clear
_shut_down();
} /**
* thread fun
*/
void *work_fun(void *arg){
char *data = (char *)arg;
pthread_detach(pthread_self());
char ret[];
int http_code = mapi_push(data, ret, );
if(http_code == ){
if(strncmp(ret, "{\"errno\":0", ) != ){
print_log(DEBUG, "errno:[2] http_cdoe:[%d] data:[%s] ret:[%s]", http_code, data, ret);
}
}else{
print_log(DEBUG, "errno:[1] http_cdoe:[%d] data:[%s] ret:[%s]", http_code, data, ret);
} //printf("%d, %s, %s\n", http_code, data, ret);
//notice main thread
pthread_mutex_lock(&active_struct.active_mutex);
active_struct.active--;
pthread_cond_signal(&active_struct.active_cond);
pthread_mutex_unlock(&active_struct.active_mutex);
}
其实还有好多可以优化的点,线下执行了一下,效果和速度还行
多线程进行http请求的更多相关文章
- GCD使用dispatch_semaphore_t创建多线程网络同步请求
一.简介: dispatch_semaphore_t:表示信号,生成信号的方法是 dispatch_semaphore_t semaphore= dispatch_semaphore_create(0 ...
- 多线程时,请求执行不是按顺序的,可添加Critical Section Controller(临界部分控制器),执行顺序是固定的,但执行一段时间后,该逻辑器下的请求不再循环,无解ing
- 用Python写的一个多线程机器人聊天程序
本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...
- PHP 多线程、多进程
多线程:PHP其实并不支持多线程,只是通过一些扩展或者socket方式伪装成多线程,实质不是的.在PHP 5.3 以上版本,使用 pthreads PHP扩展,可以使PHP真正地支持多线程:或者使用 ...
- 如何在http请求中使用线程池(干货)
这段时间对网络爬虫比较感兴趣,实现起来实际上比较简单.无非就是http的web请求,然后对返回的html内容进行内容筛选.本文的重点不在于这里,而在于多线程做http请求.例如我要实现如下场景:我有N ...
- Python Socket请求网站获取数据
Python Socket请求网站获取数据 ---阻塞 I/O ->收快递,快递如果不到,就干不了其他的活 ---非阻塞I/0 ->收快递,不断的去问,有没有送到,有没有送到,. ...
- 使用Charles抓取APP之HTTPS请求
Charles是一款非常好用的抓包工具,通常使用它来进行APP开发抓包调试,尤其是HTTPS请求. 一.安装Charles 去官网(https://www.charlesproxy.com/)下载软件 ...
- Android多线程下载
所用知识点: 1.设置http协议字段Range “bytes=“start+”-”+end conn.addRequestProperty("Range", "byte ...
- http请求及缓存框架 GalHttprequest
GalHttprequest 是一个android平台上一个轻量级的http网络请求及缓存框架.当前GalHttpRequest支持以下功能: 同步请求Stirng.InputStream.Bitma ...
随机推荐
- iOS KVO概述
iOS KVO概述 面试中经常会被问到:什么是KVO?这个问题既然出现概率这么大,那么我们就来详细讲一讲到底什么是KVO.下次再有面试官问你的时候,你就可以娓娓道来,以彰显高逼格 概述 问:什么是KV ...
- WPF学习之路(三) 属性与依赖
类型是DependencyProperty的属性是依赖属性 依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值. 为什么引入依赖属性: MSDN原文 One of the p ...
- 哭瞎!360云盘将关停,你的几十T照片和文件该怎么办
IDO老徐刚得到了一个非常不开心的消息,360云盘将停止个人云盘服务...进行业务转型,在网盘存储.传播内容的合法性和安全性得到彻底解决之前不再考虑恢复,之后转型企业云服务. 而且之前共享的所有资料, ...
- Linux中如何解压iso类型文件
在Linux下如何解压iso类型的文件呢? 可以使用mount命令来处理 [root@DB-Server tmp]# ls /tmp/rhel-server-5.7-x86_64-dvd.iso /t ...
- Hadoop生态圈以及各组成部分的简介
1.Hadoop是什么? 适合大数据的分布式存储与计算平台 HDFS: Hadoop Distributed File System分布式文件系统 MapReduce:并行计算框架 解决的问题: HD ...
- 说完Pivot 今天说下Unpivot 的处理方式
上次说到,既然有Pivot 的行转列,那么肯定也有Unpivot 的列转行 .其实unpivot 处理的情况也是差不多,也是分3步走. 首先也是先演示一下unpivot 的用法 ),Mon TIME, ...
- Spark SQL 之 Migration Guide
Spark SQL 之 Migration Guide 支持的Hive功能 转载请注明出处:http://www.cnblogs.com/BYRans/ Migration Guide 与Hive的兼 ...
- Activiti之 Exclusive Gateway
一.Exclusive Gateway Exclusive Gateway(也称为XOR网关或更多技术基于数据的排他网关)经常用做决定流程的流转方向.当流程到达该网关的时候,所有的流出序列流到按照已定 ...
- gulp系列:简单实践
coffescript测试源码 gulp = require('gulp') #删除 1.清空目录 常用插件 gulp-clean .del (nodejs模块) del = require('d ...
- [转]输出带颜色的shell
格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...