Comments (7)
把测试代码附上,这个图看不出来什么。
from cinatra.
简化后的代码:
#include "cinatra.hpp"
#include <chrono>
#include <fstream>
namespace Coro = async_simple::coro;
using namespace cinatra;
Coro::Lazy<void> test(std::string endpoint)
{
using namespace std::chrono_literals;
coro_http_client client;
client.set_conn_timeout(3s);
client.set_req_timeout(2s);
auto result = co_await client.async_post(
endpoint,
"{\"xxx\": \"xxx\"}",
req_content_type::json);
if (result.status == 200) {
std::cout << "OK\n";
} else {
std::cout << "Error\n";
}
co_return;
}
Coro::Lazy<void> test1()
{
std::ifstream infile("apis.txt");
std::string line;
std::vector<Coro::Lazy<void>> tasks;
if (infile.is_open()) {
while(std::getline(infile, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
if (!line.empty()) {
tasks.push_back(test(line));
}
}
}
co_await Coro::collectAll(std::move(tasks));
co_return;
}
int main()
{
Coro::syncAwait(test1());
return 0;
}
apis.txt 文件内容:
http://103.152.35.2:1188/translate
http://49.233.41.73:1188/translate
http://101.43.76.234:1188/translate
http://49.232.164.78:1188/translate
http://1.12.243.147:1188/translate
http://82.156.36.11:1188/translate
http://106.14.17.223:1188/translate
http://82.157.137.187:1188/translate
http://8.142.134.155:1188/translate
http://132.145.80.159:1188/translate
from cinatra.
好的,晚点看一下
from cinatra.
txt文件默认换行符号是\r\n。windows系统采用的标准的换行格式\r\n,所以读文件的时候会直接去掉\r\n。
linux采用的非标准格式,所有读出来的文件末尾多了一个\r符号。
cinatra解析url的path的时候会判断字符,出现非path字符会直接退出。
因此修改为如下代码即可:
后续改进方案cinatra会在url值填入前去除\r\n等非path符号
#include <chrono>
#include <fstream>
#include <iostream>
#include "cinatra.hpp"
namespace Coro = async_simple::coro;
using namespace cinatra;
Coro::Lazy<void> test(std::string endpoint) {
using namespace std::chrono_literals;
std::cout << "endpoint is: " << endpoint << std::endl;
coro_http_client client;
client.set_conn_timeout(3s);
client.set_req_timeout(2s);
auto result = co_await client.async_post(endpoint, "{\"xxx\": \"xxx\"}",
req_content_type::json);
if (result.status == 200) {
std::cout << "OK\n";
}
else {
std::cout << "Error\n";
}
co_return;
}
Coro::Lazy<void> test1() {
std::ifstream infile("apis.txt");
std::string line;
std::vector<Coro::Lazy<void>> tasks;
if (infile.is_open()) {
while (std::getline(infile, line)) {
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
line.erase(std::remove_if(line.begin(), line.end(),
[](char c) {
return c == '\r';
}),
line.end());
if (!line.empty()) {
tasks.push_back(test(line));
}
}
}
co_await Coro::collectAll(std::move(tasks));
co_return;
}
int main() {
Coro::syncAwait(test1());
return 0;
}
from cinatra.
简化后的代码:
#include "cinatra.hpp" #include <chrono> #include <fstream> namespace Coro = async_simple::coro; using namespace cinatra; Coro::Lazy<void> test(std::string endpoint) { using namespace std::chrono_literals; coro_http_client client; client.set_conn_timeout(3s); client.set_req_timeout(2s); auto result = co_await client.async_post( endpoint, "{\"xxx\": \"xxx\"}", req_content_type::json); if (result.status == 200) { std::cout << "OK\n"; } else { std::cout << "Error\n"; } co_return; } Coro::Lazy<void> test1() { std::ifstream infile("apis.txt"); std::string line; std::vector<Coro::Lazy<void>> tasks; if (infile.is_open()) { while(std::getline(infile, line)) { line.erase(std::remove(line.begin(), line.end(), ' '), line.end()); if (!line.empty()) { tasks.push_back(test(line)); } } } co_await Coro::collectAll(std::move(tasks)); co_return; } int main() { Coro::syncAwait(test1()); return 0; }apis.txt 文件内容:
http://103.152.35.2:1188/translate http://49.233.41.73:1188/translate http://101.43.76.234:1188/translate http://49.232.164.78:1188/translate http://1.12.243.147:1188/translate http://82.156.36.11:1188/translate http://106.14.17.223:1188/translate http://82.157.137.187:1188/translate http://8.142.134.155:1188/translate http://132.145.80.159:1188/translate
代码设置超时时间不对,req_timeout 包括连接时间的,你设置的连接超时时间比req 超时时间还短是不对的。
from cinatra.
感谢答复,我晚上再试试。
from cinatra.
已解决!
from cinatra.
Related Issues (20)
- refactor cinatra server to support c++20 coroutine HOT 4
- 编译报错,‘_M_set_length’ is not a member of ‘std::string’ HOT 5
- 如何使用已有的io_context运行cinatra HOT 1
- 如何获取coro_http_client调接口 耗时 HOT 3
- Highly concurrent downloading of 2m js files will increase memory usage HOT 1
- websocket,1分钟没有消息交互,服务端会主动把链接掐断 HOT 2
- Windows visit Chinese filename not found
- 请问下,考虑支持unix套接字吗. HOT 10
- 是否可以让server对象的listen函数支持设置连接队列的长度? HOT 6
- bug: gcc resize_and_overwrite 導致髒數據的問題 HOT 21
- fatal error: 'asio/dispatch.hpp' file not found HOT 2
- https question HOT 18
- Failed to connect websocket with `Connection: upgrade` HOT 3
- [需求] 处理程序相关 HOT 12
- 上传二进制文件无法使用 HOT 2
- async_download 没有写入内容到文件 HOT 5
- 请问下,当链接断开时,如何获取到具体原因? HOT 3
- 【错误与需求】Nginx与chunked模式 HOT 16
- Access-Control-Allow-Origin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cinatra.