多线程进行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 ...
随机推荐
- GpsLocationProvider中的sendExtraCommand方法
Android系统源码中GpsLocationProvider类中包含sendExtraCommand方法,代码如下 @Override public boolean sendExtraCommand ...
- 【代码笔记】iOS-带输入框的UIAlertView
一,效果图. 二,代码. //点击任何处,弹出输入框 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIAlertV ...
- 【代码笔记】iOS-创建具有中划线的文字
一, 效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m - (void)viewDidLoad { [super viewDidL ...
- 通过跳板机建立信任,对多个tomcat服务统一安装部署(shell编写)
unifyDeploy 自动化统一安装部署 系统版本: unifyDeploy0.1 文件编号: 0.1 发布日期: 2014-06-26 编 制: WangYong 版权所有 内部资料注意保密 ...
- .net 读写记事本文件
这是读取文件的代码 StreamReader myreader = File.OpenText(_filepath);//读取记事本文件 string s = ""; s = my ...
- Unbuntu_14.04编译openjdk7
今天有问题需要研究一下JVM,但系统挂了,只能重装.在ubuntu下再次编译JDK,大约2个半小时,将遇到的问题笔记整理一下. 1.下载Openjdk Source Code 我用的是http://d ...
- 项目管理学习笔记之五.沟通协调能力II
二.沟通模型:一个双向交流的过程 沟通模型:编 码---------------->信息-----------------> 解码&歧义发送者 ...
- Learning The Bash Shell读书笔记(整理)
最近搞了一本书 Learning Bash Shell,发现有人已经写了阅读笔记,我就在这边整理一下 来自blog:http://blog.sina.com.cn/n4mine Learning Th ...
- 正确的选择log级别
开发一个应用,日志的重要性不言而喻.然而有时会发现日志中会出现大量的垃圾日志.所谓垃圾日志,就是不需要知道的日志,或者这些日志对于应用查看.跟踪没有什么作用.也正是(但不仅仅是)出于这些问题的考量,常 ...
- Druid 介绍及配置
1. Druid是什么? Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. 2. 在哪里下载druid 正式版本下载:maven中央仓库: http://cent ...