Comments (11)
Hi, @gcclua
You can try using httpc:multi_request
like this:
local Log = require("logging"):new()
local hc = http:new {}
local ok, response_array = hc:multi_request {
[1] = {
domain = "http/https://yourdomain/path",
method = "get/post/json/file",
headers = {{key1 = value1, key2 = value2, ...}},
files/body = "your body"
},
[2] = { },
[...] = { }
}
Log:DEBUG(ok, response_array)
This is a concurrent request method that will open multiple coroutines to concurrently request array items, and the return value is also in the array.
All usage examples can be found in script/test_httpc.lua
. :)
from cfadmin.
thanks , I will try it , and i have a new idea , copy multi instance of cf , and write logic circle inside main.lua
while true do
-- get spider new task from the center cf
local hc = http:new {}
local SpiderTaskList = hc:get(CenterUrl)
local SpiderTaskSet = ParseAndFetchURLFrom(SpiderTaskList)
if IsEmpty(SpiderTaskSet) == false then
-- do each task
for k,v in pairs(SpiderTaskSet) do
local code, body = hc:get(v)
print(code, body)
end
else
Sleep(2)
end
end
We will get distribute spider ? but all logic will inside core_sys_init, before core_sys_run,
potential problems?
from cfadmin.
Good idea, there is no problem with this use.
You may need to know that hc.class
should close the connection when returning nil
. : )
from cfadmin.
OK, thank you
from cfadmin.
@gcclua
Add:
hc.class
needs to be called actively when it is used: hc:close()
method, otherwise it may cause fd
to leak(Even if you actively call collectgarbage
, it will not release fd
).
Don't forget it. : )
from cfadmin.
@gcclua
Add:
hc.class
needs to be called actively when it is used:hc:close()
method, otherwise it may causefd
to leak(Even if you actively callcollectgarbage
, it will not releasefd
).
Don't forget it. : )
Way1:
while true do
-- get spider new task from the center cf
local hc = http:new {}
local SpiderTaskList = hc:get(CenterUrl)
local SpiderTaskSet = ParseAndFetchURLFrom(SpiderTaskList)
if IsEmpty(SpiderTaskSet) == false then
-- do each task
for k,v in pairs(SpiderTaskSet) do
local code, body = hc:get(v)
print(code, body)
end
else
Sleep(2)
end
hc:close() --Close fd
end
Way2:
while true do
-- get spider new task from the center cf
local hc = http:new {}
local SpiderTaskList = hc:get(CenterUrl)
local SpiderTaskSet = ParseAndFetchURLFrom(SpiderTaskList)
if IsEmpty(SpiderTaskSet) == false then
-- do each task
for k,v in pairs(SpiderTaskSet) do
local hc = http:new {}
local code, body = hc:get(v)
print(code, body)
hc:close() --Close fd
end
else
Sleep(2)
end
hc:close() --Close fd
end
which is correct way?
from cfadmin.
According to how your code is written, you can write it like this:
local httpc = require "httpc"
local http_cls = require "httpc.class"
while true do
-- get spider new task from the center cf
local SpiderTaskList = httpc.get(CenterUrl)
local SpiderTaskSet = ParseAndFetchURLFrom(SpiderTaskList)
if noEmpty(SpiderTaskSet) then
local hc = http_cls:new {} -- new httpc class
for k,v in pairs(SpiderTaskSet) do
local hc = http_cls:new {}
local code, body = hc:get(v)
print(code, body)
end
hc:close() -- close httpc
else
Sleep(2)
end
end
And the internal domain must be guaranteed to be the same.
But using multi_request
doesn't have this limitation, and you don't have to actively call the hc:close
method. like this:
local httpc = require "httpc"
while true do
-- get spider new task from the center cf
local SpiderTaskList = httpc.get(CenterUrl)
local SpiderTaskSet = ParseAndFetchURLFrom(SpiderTaskList)
if noEmpty(SpiderTaskSet) then
local requests = {}
for k,v in pairs(SpiderTaskSet) do
requests[#requests + 1] = { domain = v }
end
local ok, response_array = httpc.multi_request(requests)
print(ok, response_array)
else
sleep(2)
end
end
Because the internal will automatically close all fd
. (Lua gc will automatically recycle the object) after each request is completed.
This is the difference between them.
from cfadmin.
great, httpc.multi_request is simple to use, I want the result will return one by one and with timeout limit , timeout as the task fail flag, in one thread so difficult to implement, has cf some help ??
from cfadmin.
httpc.multi_request
has a timeout
parameter that controls the maximum timeout for each request to time out. like this:
httpc.multi_request {
{
domain = "your domain",
timeout = 15
}
}
You can refer to this.
from cfadmin.
OK, I will use httpc.multi_request
from cfadmin.
@gcclua
Very happy to help you! : )
I suggest you use the latest version I just released, which fixes some bugs in the httpc
library.
from cfadmin.
Related Issues (20)
- sudo docker-compose -f docker-compose-with-cfadmin.yaml up 运行失败 HOT 4
- ubuntu build error HOT 4
- 自己编译和docker都不能正常运行 HOT 10
- 访问错误,出现不能继续正常工作的情况。 HOT 9
- 安全机制控制问题 HOT 2
- 文件上传只能小文件 HOT 15
- 域名访问 出现问题 HOT 19
- firefox 认证不正确 HOT 3
- 浏览器适配问题 HOT 5
- 请教个路由的问题 HOT 5
- Content-Type 最好能修改 HOT 34
- 关于多库,分权分域的问题 HOT 1
- 后台操作耗时,如何同步? HOT 11
- 升级后mysql连接错误 HOT 5
- 最新代码启动问题 HOT 5
- 目录结构有问题 HOT 1
- 经由cfadmin 后台启动的脚本 或程序 信号量被屏蔽了 HOT 23
- 发现一个稳定性的随机问题 HOT 5
- 建议httpd增加DELETE和PATCH动词的支持 HOT 1
- 大哥,AES_256_GCM加密可以用吗 HOT 3
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 cfadmin.