Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Smalltalk中重试块的优雅方式_Smalltalk - Fatal编程技术网

在Smalltalk中重试块的优雅方式

在Smalltalk中重试块的优雅方式,smalltalk,Smalltalk,Smalltalk通过使用on:retry方法定义重试之间的行为来支持重试异常。我想实现由两个附加操作组成的重试行为:delayInterval、maxAttempts [ operations ] on: Exception maxAttempts: 10 delayInterval: 5 seconds do: [ : e | Transcript show: 'Trying...'; cr ] 有什么优雅的方法可以做到这一点吗 on: exceptionClass maxAttempts

Smalltalk通过使用on:retry方法定义重试之间的行为来支持重试异常。我想实现由两个附加操作组成的重试行为:delayInterval、maxAttempts

[ operations ]
on: Exception
maxAttempts: 10
delayInterval: 5 seconds
do: [ : e | Transcript show: 'Trying...'; cr ]
有什么优雅的方法可以做到这一点吗

on: exceptionClass maxAttempts: anInteger delayInterval: seconds do: aBlock

max := anInteger.
exception := false.
[ result := self on: exceptionClass do: [ :ex | aBlock value: ex. exception := true. max := max - 1 ].
exception and: [max > 0 ] ] whileTrue: [ exception := false ].
^result

“优雅”是有争议的…

卡洛斯的方法很好,但我看到它的问题是,它多次发送
\on:do:
消息。这是不必要的,因为
异常
理解
#重试
消息。因此,我们可以在处理块内循环,而不是将所有内容都包含在一个循环中,如下所示:

BlockClosure >> on: aClass do: aBlock maxAttempts: anInteger
  | counter |
  counter := anInteger.
  ^self
    on: aClass
    do: [:ex | | result |
      result := aBlock value: ex.
      counter := counter - 1.
      counter > 0 ifTrue:[ex retry].
      ex return: result]
请注意,此代码中没有“语法”循环。然而,执行流将根据
ex retry
消息再次评估接收块(不到达
ex return:result
行)。只有当
计数器
达到
0
时,处理程序才会“放弃”并返回异常块的值
aBlock

同样的想法现在也可用于引入超时:

on: aClass
do: aBlock
maxAttempts: anInteger
timeout: anotherInteger
  | counter timeout |
  counter := anInteger.
  timeout := TimeStamp now asMilliseconds + anotherInteger "dialect dependent".
  ^self
    on: aClass
    do: [:ex | | result |
      result := aBlock value: ex.
      counter := counter - 1.
      (counter > 0 and: [TimeStamp now asMilliseconds < timeout])
         ifTrue: [ex retry].
      ex return: result]
on:aClass
do:一个锁
maxtures:一个整数
超时:另一个Integer
|计数器超时|
计数器:=整数。
超时:=时间戳现在为毫秒+另一个Integer“方言相关”。
^自我
关于:aClass
do:[:ex | result|
结果:=锁定值:ex。
计数器:=计数器-1。
(计数器>0和:[时间戳现在为毫秒<超时])
ifTrue:[重试后]。
交回:结果]

同样的考虑也适用于这里。唯一的区别是,
重试
条件也考虑了超时限制。

您尝试过什么吗?是否希望在每次重试之前执行
do:
之后的块?比如,操作,延迟,do,操作,延迟,do,…?你针对的是哪种Smalltalk方言?任何方言都可以