gpt4 book ai didi

haskell - 为什么 `mask_` 会中和 `timeout` ?

转载 作者:行者123 更新时间:2023-12-02 23:25:52 25 4
gpt4 key购买 nike

我终于能够跟踪一个奇怪的错误,我遇到了(至少对我来说)masktimeout 之间令人惊讶的交互:

import System.Timeout
import Control.Exception

ack :: Int -> Int -> Int
ack m n | m == 0, n >= 0 = n + 1
| m > 0, n == 0 = ack (m - 1) 1
| m > 0, n > 0 = ack (m - 1) (ack m (n - 1))

tryack :: Int -> Int -> IO (Maybe Int)
tryack m n = timeout 100000 {- uS -} $ evaluate $ ack m n

main :: IO ()
main = do
a <- tryack 3 11
print a -- Nothing

b <- mask_ $ tryack 3 11
print b -- Just 16381 after a few seconds

这让我觉得这是一种相当“非组合”的交互,因为这意味着如果一个库在内部使用超时,则在调用的某处外部应用掩码 -链可能会导致库发生故障。

那么这是超时实现中的(已知)缺陷还是故意的?

最佳答案

mask_ 是什么意思?做什么?

Executes an IO computation with asynchronous exceptions masked. That is, any thread which attempts to raise an exception in the current thread with Control.Exception.throwTo will be blocked until asynchronous exceptions are unmasked again.

timeout 是什么意思?做什么?

Wrap an IO computation to time out and return Nothing in case no result is available within n microseconds (1/10^6 seconds).In case a result is available before the timeout expires, Just a is returned. ... A tricky implementation detail is the question of how to abort an IO computation. This combinator relies on asynchronous exceptions internally.

所以... mask_ 正在阻止 timeout 传递异常。事情就是这样。

你不能使用mask并且让timeout起作用。

也许更好的方法是使用处理程序来捕获除 timeout 使用的异常之外的任何内容?

关于haskell - 为什么 `mask_` 会中和 `timeout` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6324334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com