async wait / non blocking wait in python

13,509

Unless there is something preventing you from doing so, just put it into a thread.

import threading
import time

class Typewriter(threading.Thread):
    def __init__(self, your_string):
        threading.Thread.__init__(self)
        self.my_string = your_string

    def run(self):
        for char in self.my_string:
            libtcod.console_print(0,3,3,char)
            time.sleep(50)

# make it type!
typer = Typewriter(your_string)
typer.start()
# wait for it to finish
typer.join()

This will prevent the sleep blocking your main function.

The documentation for threading can be found here
A decent example can be found here

Share:
13,509
mahnouel
Author by

mahnouel

Artist and Coder, loves StarWars AND StarTrek.

Updated on July 20, 2022

Comments

  • mahnouel
    mahnouel almost 2 years

    i like to output each letter of a string after waiting some time, to get a typewriter effect.

    for char in string:
         libtcod.console_print(0,3,3,char)
         time.sleep(50)
    

    But this blocks the main thread, and the program turns inactive.
    You cant access it anymore until it finishes
    Note: libtcod is used