# 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:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [August 3, 2021, 11:21pm UTC](https://discourse.julialang.org/t/how-to-timeout-in-a-function-call/65763/1 "2021-08-03T23:21:50Z")

</div>

I am currently downloading some data from a remote server, however the connection may not always be stable and therefore the data loading might hang. Therefore, I would like break the connection and restart it after a certain period of time. How do I do this?

Say, I want to do:

```julia
NCDatasets.load!(OPeNDAP URL HERE,tmparray,:,:,1)

```

How do I set a condition outside such that if this function doesn’t finish executing in 10 seconds, I break the loop and try it again?

---

<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.
