2013-05-22 6 views
1

J'aime sortir chaque lettre d'une chaîne après avoir attendu un certain temps, pour obtenir un effet de machine à écrire.attente asynchrone/non bloquante attente en python

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

Mais cela bloque le thread principal, et le programme devient inactif.
Tu ne peux pas y accéder plus jusqu'à ce qu'il se termine
Note: libtcod est utilisé

+0

semble que vous avez besoin de regarder [multithreading] (http://docs.python.org/2/library/threading.html) ou [Multiprocessing] (http://docs.python.org/2/library/multiprocessing.html). –

Répondre

3

À moins qu'il ya quelque chose qui vous empêche de le faire, il suffit de mettre dans un fil.

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() 

Ceci empêchera la veille de bloquer votre fonction principale.

La documentation pour le filetage peut être found here
Un exemple décent peut être found here