简体   繁体   English

微调器类线程错误-仅允许一个线程

[英]spinner class thread error - only one thread allowed

I already asked a question about this yesterday, but now I have another :) im trying to write a text effects type class for my terminal apps to use - it does stuff like position cursor, clear screen, colour selection inside the echo string with colours preceded by an '@', random case, colour cylcling and other fun stuff (partly to aid my learning of python and partly for usefulness) - if I wanted to have parts of my class not be a thread how would I do it ? 我已经问了一个问题关于这个昨天,但现在我有另一个:)我尝试写一个文字特效类型类为我的终端应用程序使用-它的东西像位置的光标,画面清晰,回声串色内部颜色选择前面有一个'@',随机大小写,颜色循环和其他有趣的东西(部分是为了帮助我学习python,部分是出于实用性)-如果我想让班级的一部分不成为线程,我该怎么办? at the moment I have this spinner thing working (whole code here ) but I want to call it multiple times. 目前,我有这个微调器工作( 这里是完整代码),但我想多次调用它。 like this: 像这样:

s=spin()
s.echo('@@') # clears screen
# at the moment - here i get an error because only one thread can be started
s.echo('@red this is red @green green etc...')

the echo function does a lot of stuff as you can see if you look at the pastebin, so i need to call that quite a bit, but multiple calls result in 'only one thread' allowed error. 如果您查看pastebin,则echo函数会做很多事情,因此我需要调用很多,但是多次调用会导致“仅一个线程”错误。 perhaps i should be doing it a different way. 也许我应该以不同的方式来做。 This was the basic old code before the pastebin stuff. 这是pastebin之前的基本旧代码。

spinner="▏▎▍▌▋▊▉█▉▊▌▍▎" #utf8

#convert the utf8 spinner string to a list
chars=[c.encode("utf-8") for c in unicode(spinner,"utf-8")]

class spin(threading.Thread):

    def __init__(self):
        super(spin,self).__init__()
        self._stop = False

    def run (self):
        pos=0
        while not self._stop:
            sys.stdout.write("\r"+chars[pos])
            sys.stdout.flush()
            time.sleep(.15)
            pos+=1
            pos%=len(chars)

    def cursor_visible(self):
        os.system("tput cvvis")
    def cursor_invisible(self):
        os.system("tput civis")
    def stop(self):
        self._stop = True
    def stopped(self):
        return self._stop == True

Only the run method is actually running in a diffrent thread. 实际上,仅run方法在其他线程中运行。 The problem with your code is that you try to start the thread more than one time (in the echo method). 代码的问题是您尝试多次启动线程(在echo方法中)。 You should check if the thread is already started (look at self._stop) before starting. 在启动之前,您应该检查线程是否已经启动(请查看self._stop)。

def echo (self, arg=None, sep=' ', end='\n', rndcase=True, txtspeed=0.03, bnum=0, spsw=True):
    print cursave, 
    self.cursor_invisible()

    # do you have to do all this ? 
    # c0m4: Only if you need all of those to be accessible for the spin object when echo ends
    self.arg=arg
    self.sep=sep
    self.end=end
    self.rndcase=rndcase
    self.txtspeed=txtspeed
    self.bnum=bnum
    self.spsw=spsw

    pos=0
    cmd, txt = [reset], []
    spsw=True
    last_colour=''
    txtpos=0
    if arg:

# test if spinner is wanted and set the text position to be moved across a bit to allow for it
    if spsw is True:
    txtpos=4
    self.start()

The last line if where the troubles start. 最后一行,如果麻烦从哪里开始。 It is not possible to start a thread more then once. 启动一个线程不能超过一次。 Therefore you need to check if the thread is running before you restart it. 因此,您需要在重新启动线程之前检查线程是否正在运行。 Add something like 添加类似

self.running = False

to your init method, and then set it in the run method 到您的init方法,然后在run方法中进行设置

self.running = True

Then you can check the status of the object (thread running or not) like this: 然后,您可以像下面这样检查对象的状态(是否正在运行线程):

if not self.running:
    self.start()

If you need to run the initial portion of run regardless, you should put that in a separate method, like this: 如果无论如何都需要运行run的初始部分,则应将其放在单独的方法中,如下所示:

def resetThread():
    self.pos = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM