# How to timeout in a function call

**URL:** <https://discourse.julialang.org/t/how-to-timeout-in-a-function-call/65763>\
**Category:** General Usage\
**Created:** [August 3, 2021, 11:21pm UTC](https://discourse.julialang.org/t/how-to-timeout-in-a-function-call/65763 "2021-08-03T23:21:50Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 4, 2021, 11:50am UTC](https://discourse.julialang.org/t/how-to-timeout-in-a-function-call/65763/2 "2021-08-04T11:50:24Z")

</div>

Potentially useful:

> [@Help writing a timeout macro](https://discourse.julialang.org/t/help-writing-a-timeout-macro/16591/5):
>
> I’ll just leave my approach here… macro timeout(expr, seconds=-1, cb=(tsk) -\> Base.throwto(tsk, InterruptException())) quote tsk = @task $expr schedule(tsk) if $seconds \> -1 Timer((timer) -\> $cb(tsk), $seconds) end return fetch(tsk) end end julia\> @timeout (sleep(3); println("done")) 3.1 done julia\> @timeout (sleep(3); println("done")) 3 ERROR: TaskFailedException: InterruptException: Stacktrace: [1] try\_yieldto(::typeof(Base.ensu…

Unless you can use `download` for this task.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-timeout-in-a-function-call/65763)._
