Python 在多线程程序中控制解释器
在python解释器模式下,我启动了一个线程,该线程指向在无限循环中打印语句的函数。现在我想再次控制解释器来启动另一个线程。我怎样才能回到interperter>>?让我们假设这是您的代码(): 然后,为了将打印发送到控制台,您可以将输出重定向到文件:Python 在多线程程序中控制解释器,python,Python,在python解释器模式下,我启动了一个线程,该线程指向在无限循环中打印语句的函数。现在我想再次控制解释器来启动另一个线程。我怎样才能回到interperter>>?让我们假设这是您的代码(): 然后,为了将打印发送到控制台,您可以将输出重定向到文件: def run(self): f = open('/tmp/workfile{0}.txt'.format(self.num), 'r+') while true:
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
类MiThread(threading.Thread):
定义初始化(self,num):
threading.Thread.\uuuuu init\uuuuuu(自)
self.num=num
self.status=“”
def运行(自):
尽管如此:
self.status=“Soy el hilo{0}”。格式(self.num)
t1=多线程(i)
t1.start()
t2=MiThread(i)#解释器在那里。但是它被打印隐藏了。当我的第一个线程打印时,我似乎无法控制和执行命令!尝试重定向输出INSTAT以将其发送到控制台。代码已准备就绪。
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