<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          TokioRust 異步編程框架

          聯(lián)合創(chuàng)作 · 2023-09-29 12:20

          Tokio 是 Rust 中的異步編程框架,它將復(fù)雜的異步編程抽象為 Futures、Tasks 和 Executor,并提供了 Timer 等基礎(chǔ)設(shè)施。Tokio 快速、可靠,且可擴(kuò)展。

          Tokio 是一個(gè)事件驅(qū)動(dòng)的非阻塞 I/O 平臺(tái),用于使用 Rust 編程語言編寫異步應(yīng)用。在高層設(shè)計(jì)上,它提供了一些主要組件:

          • 多線程、工作竊?。╳ork-stealing)的 task scheduler 。

          • 由操作系統(tǒng)的事件隊(duì)列(epoll,kqueue,IOCP 等)支撐的 reactor 。

          • 異步 TCP 和 UDP 套接字。

          這些組件提供構(gòu)建異步應(yīng)用所需的運(yùn)行時(shí)組件。

          示例

          extern crate tokio;
          
          use tokio::prelude::*;
          use tokio::io::copy;
          use tokio::net::TcpListener;
          
          fn main() {
              // Bind the server's socket.
              let addr = "127.0.0.1:12345".parse().unwrap();
              let listener = TcpListener::bind(&addr)
                  .expect("unable to bind TCP listener");
          
              // Pull out a stream of sockets for incoming connections
              let server = listener.incoming()
                  .map_err(|e| eprintln!("accept failed = {:?}", e))
                  .for_each(|sock| {
                      // Split up the reading and writing parts of the
                      // socket.
                      let (reader, writer) = sock.split();
          
                      // A future that echos the data and returns how
                      // many bytes were copied...
                      let bytes_copied = copy(reader, writer);
          
                      // ... after which we'll print what happened.
                      let handle_conn = bytes_copied.map(|amt| {
                          println!("wrote {:?} bytes", amt)
                      }).map_err(|err| {
                          eprintln!("IO error {:?}", err)
                      });
          
                      // Spawn the future as a concurrent task.
                      tokio::spawn(handle_conn)
                  });
          
              // Start the Tokio runtime
              tokio::run(server);
          }
          瀏覽 23
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费毛片看片 | 国产婷婷欧美 | 天天搞天天干天天日 | aa片电影免费观看 | 中文字幕日韩成人电影 |