# Break execution after a given time

**URL:** <https://discourse.julialang.org/t/break-execution-after-a-given-time/29233>\
**Category:** General Usage\
**Tags:** optimization\
**Created:** [September 27, 2019, 1:19pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233 "2019-09-27T13:19:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [September 27, 2019, 1:19pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/1 "2019-09-27T13:19:52Z")

</div>

Hi,

I am optimization a function choosing different parameters. However, for some parameters, it will loop forever. So I want to set a timer like following: (just an example)

```julia
function optim(theta)
    if run time of function x(theta) > 1min
         return inf 
   else return x(theta)
end

```

Thanks!  
Ethan

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 27, 2019, 1:53pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/2 "2019-09-27T13:53:36Z")

</div>

(please quote your code)

Query the time with `time_ns()`, save it, then compare.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 27, 2019, 3:48pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/3 "2019-09-27T15:48:21Z")

</div>

It seems like you will need to do this within the function itself - otherwise how will you interrupt it? (Maybe some kind of threading?)

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [September 27, 2019, 4:28pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/4 "2019-09-27T16:28:56Z")

</div>

I’ve done something like this before in an extremely hacky way, which I’ll describe below. I would wager good money there’s something better with tasks and threads and I’d be excited to learn it.

1. Set up a channel, but don’t put anything in it. The function you’ll interrupt needs to take a channel as an argument.
2. Modify your function code so that it periodically checks if the channel has a value; if so, the function immediately returns.
3. Create two tasks: one to execute the function, and one to sleep for a minute before adding a value to the channel.

That way, after ~1 minute the channel will have a value, and the next time the long-running function checks the channel, it’ll immediately return.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 27, 2019, 5:59pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/5 "2019-09-27T17:59:36Z")

</div>

this actually doesn’t sound too bad…and it also have the plus side that, you can first don’t push anything into the channel, and then manually interrupt it — I guess it combines check `time_ns()` and just have a new REPL running.

---

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 1, 2019, 8:47pm UTC](https://discourse.julialang.org/t/break-execution-after-a-given-time/29233/6 "2019-10-01T20:47:20Z")

</div>

that sounds doable. I will try. Thx!
