【发布时间】:2018-03-05 14:14:13
【问题描述】:
我的代码需要“并行”运行(不是真的,我知道 Crystal 不支持并行)。
require "http/client"
thread_count = 4
resps = [] of HTTP::Client::Response
mutex = Thread::Mutex.new
urls = [] of String
(1..10).each { |i| urls << "http://httpbin.org/delay#{i}"}
threads = Array.new(thread_count) {
Thread.new do
while url = mutex.synchronize { urls.pop? }
resp = HTTP::Client.get(url)
mutex.synchronize { resps << resp }
end
end
}
threads.map(&.join)
Thread 类的源文件说不要使用它。无论如何,这段代码不适用于HTTP::Client。
【问题讨论】:
标签: crystal-lang