安装 async

$ cabal install async
async-2.2.1 installed

async / wait / concurrently

  • async :: IO a -> IO (Async a)

    启动新线程,执行异步操作。
  • wait :: Async a -> IO a

    等待异步操作完成,并返回值。
  • concurrently :: IO a -> IO b -> IO (a, b)

    并发(concurrently)运行两个 IO 操作,返回两个结果。

示例

安装 http-conduit

$ cabal install http-conduit
Installed http-conduit-2.3.2

Sample code to accompany the book "Parallel and Concurrent Programming in Haskell"

module GetURL (getURL) where

import Network.HTTP.Conduit
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as L getURL :: String -> IO ByteString
getURL url = L.toStrict <$> simpleHttp url

一个下载模块 getURL

import GetURL

import Control.Concurrent.Async
import qualified Data.ByteString as B main = do
a1 <- async (getURL "http://www.wikipedia.org/wiki/Shovel")
a2 <- async (getURL "http://www.wikipedia.org/wiki/Spade")
r1 <- wait a1
r2 <- wait a2
print (B.length r1, B.length r2) -- (87653,58155)
  • 使用 async 开启两个线程同时下载两个网页。
  • 使用 await 等待下载结束并得到结果。
import GetURL

import Control.Concurrent.Async
import qualified Data.ByteString as B main = do
(r1, r2) <- concurrently
(getURL "http://www.wikipedia.org/wiki/Shovel")
(getURL "http://www.wikipedia.org/wiki/Spade")
print (B.length r1, B.length r2) -- (87653,58155)
  • 使用 concurrently 开启两个线程同时下载两个网页。下载结束时返回结果。

最新文章

  1. codevs 1164 统计数字
  2. struts2学习笔记之八:Action中方法的动态调用
  3. Activiti系列——如何在eclipse中安装 Activiti Designer插件
  4. 右下角显示提示窗口(New-Object,COM)
  5. Linux 内核无线子系统
  6. 《android开发艺术探索》读书笔记(八)--WindowManager
  7. 设计模式 --&gt; MVC,MVP 和 MVVM 的图示
  8. 学号:201621123032 《Java程序设计》第12周学习总结
  9. patA1059 Prime Factors
  10. Docker 空间大小设置 - 十
  11. WM_COMMAND消息
  12. Java框架部分---面试题
  13. Apollo的Oracle适配
  14. SQL-17 获取当前(to_date=&#39;9999-01-01&#39;)薪水第二多的员工的emp_no以及其对应的薪水salary
  15. WebDriver高级应用实例(7)
  16. CVE-2012-4792Microsoft Internet Explorer 释放后使用漏洞
  17. 【apt install】Unable to locate package python3-pip
  18. Spring AOP底层原理
  19. Git同时push到多个远程仓库
  20. codeforces785E

热门文章

  1. switch case 变量初始化问题
  2. 查看hbase中的中文
  3. OpenGL中移动单位中的‘单位’指什么
  4. jmx - first demo
  5. 什么是Apache Flink
  6. Ubuntu下RabbitMQ安装
  7. SpringBoot 实现前后端分离的跨域访问(CORS)
  8. jsfiddle修改个人头像
  9. Java的字符串
  10. C#打印0到100的素数