从频道读取还是超时?

时间:2016-06-14 19:21:59

标签: multithreading rust channels

使用Rust 1.9,我想阅读mpsc::channel 超时。是否有明确的习惯使这项工作?我已经看到mpsc::Select中描述的不稳定方法,但this Github discussion表明它不是一种强有力的方法。是否有更好的推荐方法来实现接收或超时语义?

2 个答案:

答案 0 :(得分:5)

我不知道你是如何使用标准库频道的,但chan crate提供了chan_select!宏:

#[macro_use]
extern crate chan;

use std::time::Duration;

fn main() {
    let (_never_sends, never_receives) = chan::sync::<bool>(1);
    let timeout = chan::after(Duration::from_millis(50));

    chan_select! {
        timeout.recv() => {
            println!("timed out!");
        },
        never_receives.recv() => {
            println!("Shouldn't have a value!");
        },
    }
}

答案 1 :(得分:3)

Rust 1.12介绍了Receiver::recv_timeout

from scrapy.utils.project import get_project_settings
from scrapy.crawler import CrawlerProcess

setting = get_project_settings()
process = CrawlerProcess(setting)

for spider_name in process.spiders.list():
    print ("Running spider %s" % (spider_name))
    process.crawl(spider_name,query="dvh") #query dvh is custom argument used in your scrapy

process.start()