# Sleeping whether or not async

**URL:** <https://trio.discourse.group/t/sleeping-whether-or-not-async/324>\
**Category:** Help and advice\
**Created:** [August 2, 2020, 5:59pm UTC](https://trio.discourse.group/t/sleeping-whether-or-not-async/324 "2020-08-02T17:59:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lagbolt](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/lagbolt/32/237_2.png) [@lagbolt](https://trio.discourse.group/u/lagbolt)\
**Post date:** [August 2, 2020, 5:59pm UTC](https://trio.discourse.group/t/sleeping-whether-or-not-async/324/1 "2020-08-02T17:59:37Z")

</div>

I’d like to be able to write some utility routines that can be called either within a ‘normal’ environment or within an async environment. I need the utility routines to be able to sleep – they scrape web sites and I don’t want to overload the sites.

Is there any way to write code to sleep which will work in both environments?

Related: is there some way to tell when you are running within a trio.run()?

---

<div class="post-metadata">

**Author:** ![agronholm](https://yyz2.discourse-cdn.com/free1/user_avatar/trio.discourse.group/agronholm/32/197_2.png) [@agronholm](https://trio.discourse.group/u/agronholm)\
**Post date:** [August 2, 2020, 8:32pm UTC](https://trio.discourse.group/t/sleeping-whether-or-not-async/324/2 "2020-08-02T20:32:05Z")

</div>

If you’re in a coroutine function, you have to use an async sleep function. Likewise, if you’re in a synchronous function, you cannot use the async sleep functions.

To answer your second question, you can either use the [sniffio](https://pypi.org/project/sniffio/) library to detect which async event loop you’re running in, or you could try calling `trio.lowlevel.current_trio_token()` which will raise a `RuntimeError` if you’re not running trio.
