2011-12-31 4 views
1

파이썬 인터프리터 모드에서 나는 무한 루프에서 명령문을 인쇄하는 함수를 가리키는 스레드를 시작했다. 이제 나는 인터프리터를 다시 제어하여 다른 스레드를 시작하고 싶습니다. 어떻게 인터 퍼터로 돌아갈 수 있습니까? 콘솔로 전송 인쇄에 insteat, 파일로 출력을 리디렉션 할 수 있습니다, 그리고다중 스레드 프로그램에서 인터프리터 제어하기

import threading 

class MiThread(threading.Thread): 
     def __init__(self, num): 
      threading.Thread.__init__(self) 
      self.num = num 

     def run(self): 
      while true: 
      print "Soy el hilo", self.num 

print "Soy el hilo principal" 

for i in range(0, 10): 
    t = MiThread(i) 
    t.start() 

:이 그

+1

인터프리터가 있습니다. 그러나 그것은 지문에 의해 숨겨져 있습니다. – danihp

+0

첫 번째 스레드가 인쇄 할 때 명령을 실행하고 제어 할 수 없습니다! – delita

+1

출력 insteat를 콘솔로 보내려면 리디렉션을 시도하십시오. – danihp

답변

2

하자 supose는 코드 (from http://mundogeek.net/ ¿QUÉ SON LOS PROCESOS Y LOS THREADS?)입니다

 def run(self): 
      f = open('/tmp/workfile{0}.txt'.format(self.num), 'r+') 
      while true: 
      f.write("Soy el hilo {0}\n".format(self.num)) 

하거나 만들 수 있습니다 스레드 상태 정보를 반환하는 스레드 메서드/속성 :

class MiThread(threading.Thread): 
     def __init__(self, num): 
      threading.Thread.__init__(self) 
      self.num = num 
      self.status = '' 

     def run(self): 
      while true: 
      self.status = "Soy el hilo {0}".format(self.num) 


t1 = MiThread(i) 
t1.start() 
t2 = MiThread(i) #<-- at this point you get back interpreter 
t2.start() 
print t1.status 
+0

+1의 연구 노력 : – hochl

+0

이 작품은 훌륭합니다! – delita

관련 문제