且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python中的异步等待/非阻塞等待

更新时间:2021-07-17 09:37:58

除非有什么阻止你这样做,把它放到一个线程中.

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.

线程文档可以在这里找到
一个不错的例子可以是在这里找到