我想完成 race_
功能,但我不知道该怎么做。
这是我现在的代码:
mainLoop :: Handle -> String -> IO ()
mainLoop handle username = do
_ <- race_ fromServer toServer
putStrLn "Race is done"
return ()
where
fromServer = forever $ do
line <- hGetLine handle
putStrLn $ "Server: " ++ line
toServer = do
line <- getLine
case line of
":quit" -> do
--hPutStrLn handle "blabla"
return ()
_ -> do
hPutStrLn handle $ line
toServer
如果我取消注释行 hPutStrLn handle "blabla"
用户将看到 Race is done
当他输入 :quit
.如果没有这一行,当用户输入 :quit
时,程序就会挂起。 .
我觉得用hPutStrLn handle "blabla"
不对只是为了完成 race_
.我假设存在一个更好的函数来完成 race_
功能。有什么想法吗?
我觉得一个简单的 return ()
很奇怪不起作用,因为 hPutStrLn
和 return
返回一个空的 IO 操作。
可以找到完整的上下文 here .句柄由 handle <- connectTo "localhost" (PortNumber 4000)
创建.服务器创建于 server.hs .我在 Windows 7 的“Git bash”(msysgit) 中使用 runhaskell
运行此代码.
最佳答案
在上下文中使用
import System.IO
import Control.Concurrent.Async
import Control.Monad
main = do
h <- openFile "./a" ReadWriteMode
mainLoop h "user"
hClose h
即使没有 hPutStrLn 也能正常工作 - 你做了什么? (其中 ./a 是一个大文件,如“dmesg > a”)
关于haskell - 如何完成 race_ 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291109/