0
def countdown(t):
    while t > 0:
        print(t)
        t = t-1
        time.sleep(1.0)
        if t == 0:
            print('blast off')

T=20

So this timer works well. It does what it needs, it counts which is what I want. But when it ends it stops my program I'm running it with and does a Timeouterror. Is there a countdown that won't do this or something I can add to it?

Easton Bornemeier
  • 1,918
  • 8
  • 22
Philzeey
  • 55
  • 7
  • 2
    Is this specific to discord? I just tried the code you provided and it worked fine. – Cory Madden Aug 04 '17 at 19:30
  • 2
    This code doesn't "error" when the countdown is done -- it *stops* because, well, you don't have any code after the loop. What do you expect to happen? The error is probably being triggered by code that you haven't shown. – John Coleman Aug 04 '17 at 19:30
  • How do you call this piece of code exactly ? – Loïc Aug 04 '17 at 19:30
  • Assuming this code does raise a TimeoutError (which I'm not convinced of), can't you just do `try: ... except TimeoutError: ...` to prevent it from terminating your program entirely? – Kevin Aug 04 '17 at 19:32
  • 1
    Is this a part of a bigger program? – Luke Aug 04 '17 at 19:33
  • @everyone yeah it is, but i just realized i have to call a background task because while it counts i can't call any other function. It's part of a bigger program so I'm thinking that's why its calling a timeout error. Sorry for the trouble, I'll see if i can figure it out now knowing theren's nothing wrong with this code specifically. Thanks. – Philzeey Aug 04 '17 at 19:56
  • @Philzeey Maybe this helps? You could call another script for instance. https://stackoverflow.com/questions/15107714/wait-process-until-all-subprocess-finish – Anton vBR Aug 04 '17 at 21:29
  • @AntonvBR hmmmm maybe. I'll give it some more look. thanks. – Philzeey Aug 05 '17 at 00:36

1 Answers1

1

You mentioned both that this was a background task, and that this causes a TimeoutError. That's because D.py runs asyncio, and time.sleep is blocking, meaning that it stops all threads running while it processes. What you want is the async-friendly version, await asyncio.sleep(1.0) instead of time.sleep(1.0).

Kae
  • 648
  • 5
  • 9