# How to access the "current run’s clock"

**URL:** <https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163>\
**Category:** Help and advice\
**Created:** [April 22, 2019, 5:53am UTC](https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163 "2019-04-22T05:53:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![math2001](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/math2001/32/79_2.png) [@math2001](https://trio.discourse.group/u/math2001)\
**Post date:** [April 22, 2019, 5:53am UTC](https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163/1 "2019-04-22T05:53:31Z")

</div>

Hello!

I have a nursery that I want to cancel at `t + 2`, where `t` is only known somewhere _in_ the nursery.

Here’s what it should be doing:

```auto
async with trio.open_nursery() as nursery:
    run_scene(nursery) # exits when the scene finishes
    nursery.cancel_scope.deadline += now() + 2

```

Notice how `run_scene` is sync: all it can do is `start_soon` tasks. I want to give the tasks that `run_scene` spawned at most 2 seconds to close, once it `run_scene` has exited. I can’t cancel straight away because  
the async close tasks (whatever they are) have been `start_soon`, not awaited.

But there’s no `now()` function. Looking at the documentation for `move_on_at`, I’m not even sure that I understand how to use it.

> `cancel_scope.deadline`: An absolute time on the current run’s clock at which this scope will automatically become cancelled.

How can we know the internal time of the clock?

I looked at the source code of `move_on_after`, because it has to use this `now()` function, and it uses `_core`!

[https://github.com/python-trio/trio/blob/master/trio/\_timeouts.py#L42](https://github.com/python-trio/trio/blob/master/trio/_timeouts.py#L42)

Is there an other way to access the internal time? Because I should be able to use every regular function like `move_on_after` without using `_core`, right?

---

<div class="post-metadata">

**Author:** ![zehauser](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/zehauser/32/103_2.png) [@zehauser](https://trio.discourse.group/u/zehauser)\
**Post date:** [April 22, 2019, 6:05am UTC](https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163/2 "2019-04-22T06:05:54Z")

</div>

There is [trio.current\_time](https://trio.readthedocs.io/en/latest/reference-core.html#trio.current_time). Later on in the docs, it’s even [described as being used to implement trio.move\_on\_after](https://trio.readthedocs.io/en/latest/reference-core.html#trio.abc.Clock.current_time), although according to your post that’s not strictly true.

However, it seems to me like you should just be wrapping the `async with trio.open_nursery()` block in a `with trio.move_on_after(2)` block. Since, as you pointed out, `start_soon` and therefore `run_scene` are synchronous, `run_scene` should complete almost instantly, right? That should be good enough to implement your “two seconds after all tasks have spawned” deadline.

---

<div class="post-metadata">

**Author:** ![math2001](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/math2001/32/79_2.png) [@math2001](https://trio.discourse.group/u/math2001)\
**Post date:** [April 22, 2019, 6:32am UTC](https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163/3 "2019-04-22T06:32:53Z")

</div>

how my god, how did I miss that…

> Since, as you pointed out, `start_soon` and therefore `run_scene` are synchronous, `run_scene` should complete almost instantly, right?

That’s the thing: it doesn’t. It’s a mini game loop (one per scene).

---

<div class="post-metadata">

**Author:** ![njs](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/njs/32/11_2.png) [@njs](https://trio.discourse.group/u/njs)\
**Post date:** [April 22, 2019, 7:19am UTC](https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163/4 "2019-04-22T07:19:08Z")

</div>

> [@math2001](#):
>
> Because I should be able to use every regular function like `move_on_after` without using `_core` , right?

In fact you should be able to _implement_ most regular functions like `move_on_after` without using `_core`… the reason Trio has a `_core` subpackage internally is to collect together the, well, core code that has to have special knowledge of Trio internals and couldn’t be implemented as a third-party library. Everything that `_core` exports also gets re-exported publicly (in `trio`, `trio.hazmat`, or `trio.testing`, depending on what it is), and everything in `trio/*.py` is implemented using only these public exports. So anything you see in `trio/*.py` is stuff that you can reimplement yourself if you wanted to, using only public APIs. The point of `_core` is to make sure that code like `move_on_after` isn’t accidentally cheating and using private APIs.

There’s definitely no way you could guess this just from looking at `trio/_timeouts.py` though… I opened an issue to hopefully help the next person:

> <https://github.com/python-trio/trio/issues/1017>
>
> In this recent forum post, @math2001 was wondering how to implement something li…ke \`trio.move\_on\_after\`, so they looked at the source... and saw a reference to \`\_core.current\_time()\`, which made them think that it was using spooky private APIs: https://trio.discourse.group/t/how-to-access-the-current-run-s-clock/163
> 
> Of course, that's not the goal. Nothing in the \`trio.\_core.\*\` API is actually private – it all gets re-exported from \`trio.\*\`, \`trio.hazmat.\*\`, or \`trio.testing.\*\`, and the whole point of having a \`\_core\` API layer \[is to make sure that it \*is\* possible to implement your own versions of things like \`move\_on\_after\`\](https://trio.readthedocs.io/en/latest/design.html#a-brief-tour-of-trio-s-internals). But there's no way @math2001 could have known that when they dug into the code :-). And if \`move\_on\_after\` had used \`trio.current\_time()\` instead of \`\_core.current\_time()\`, then the confusion could have been avoided.
> 
> So probably we should do a pass through the files in \`trio/\*.py\`, and replace references to \`\_core.\*\` with \`trio.\*\` or \`trio.hazmat.\*\` as appropriate. There might be some places where this is difficult or annoying (e.g. when implementing trio itself you can't necessarily replace \`from .\_core import \<names\>\` with \`from trio import \<names\>\`, because of circular import issues), but for files like \`trio/\_timeouts.py\` this would be easy to do.
> 
> Marking "good first issue" because it's pretty straightforward. For example, in \`trio/\_timeouts.py\`, it'd be a matter of:
> 
> 1. Replacing \`from . import \_core\` with \`import trio\`
> 2. Replacing references to \`\_core.current\_time\` → \`trio.current\_time\`, \`\_core.CancelScope\` → \`trio.CancelScope\`
> 
> There are a bunch of files to look through, but for a first issue it'd be fine to just pick one to start with.
