将类中的微调框类型对象实现为子过程 [英] implementing a spinner type object in a class as a subprocess

查看:119
本文介绍了将类中的微调框类型对象实现为子过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,只是开始做课,今天,我试图做一个'spinner'对象我可以调用像这样:一个令人困惑的事情是是否使用'线程' 线程或进程。我只是在某处读到一个线程的实例成本8meg,因为这是一个简单的文本旋转器的事情,它不保证使用大量的内存。我的第一个问题是我应该使用哪个模块,我的第二个是如何实现这个在类中,所以我可以这样调用:

I'm a total beginner and only started doing classes today, and im trying to make a sort of 'spinner' object i can call something like this: One of the things im confused about is whether to use 'thread', 'threading' or 'processes'. I just read somewhere that an instance of a thread costs 8meg, as this is a simple text spinner thing, it doesnt warrant using a huge amount of memory. My first question is which module should I use, and my second is how do implement this in a class so i can call it like this:

spin.start() - starts it

spin.stop() -  stops it

spin.cursor_invisible() - turns the cursor invisible

spin.cursor_visible() - cursor visible!

我复制了一些代码并读了一些书,但我有点困惑, :我给了一些意见,以表明我是多么无知。我一直在阅读很多,虽然,诚实!

I copied some code and read some books but Im a bit confused, what I have so far is this: i put some comments in to show how ignorant I am. I have been reading a lot though, honest! Its kind of a large thing to get your head around.

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

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

class spin():   # not sure what to put in the brackets was (threading.Thread, but now im not sure whether to use processes or not)

    def __init__(self):
            super(spin, self).__init__() # dont understand what this does
            self._stop = threading.Event()

    def run (self):
            threading.Thread(target = self).run()
            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.set() #the underscore makes this a private variable ?
    def stopped(self):
            return self._stop.isSet()


推荐答案

我已经稍微修改了你的代码。现在它运行!首先是注释版本:

I have altered your code slightly. Now it runs! First a commented version:

第一行告诉python这个源文件包含utf-8个字符

The first line tells python that this source file contains utf-8 characters

# -*- coding: utf-8 -*- 

你需要导入所有的东西,你最终会使用。你不必这样在文件的顶部,像这样,但我是一个C的家伙,这是我喜欢它...

Then you need to import all the stuff that you will eventually use. You dont have to do it at the top of the file like this, but I'm a C guy and this is how I like it...

import threading
import sys
import time
import os

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):   # not sure what to put in the brackets was (threading.Thread, but now im not sure whether to use processes or not)

线程适用于此

    def __init__(self):
        super(spin,self).__init__() # dont understand what this does

因为你正在重写threading.Thread的 init 方法,你需要调用父类的init来确保对象正确启动。

Since you are overriding the init method of threading.Thread with your own init you need to call the parent class's init to make sure the object is properly initiated.

        self._stop = False


$ b b

我将它改为了布尔值。这个threading.Event是overkill为这。

I changed this to a boolean. The threading.Event is overkill for this.

    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  #the underscore makes this a private variable ?

它实际上不是私人的,下划线只是告诉大家,它的坏形式来接受它。

Sort of. It's not actually private, the underscore just tells everyone that it's bad form to acces it.

    def stopped(self):
        return self._stop == True

最后对代码进行小测试:

And finally a small test of the code:

if __name__ == "__main__":
    s = spin()
    s.cursor_invisible()
    s.start()
    a = raw_input("")
    s.stop()
    s.cursor_visible()

这里是取消注释的版本...

And here is the uncommented version...

# -*- coding: utf-8 -*- 

import threading
import sys
import time
import os

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):   # not sure what to put in the brackets was (threading.Thread, but now im not sure whether to use processes or not)

    def __init__(self):
        super(spin,self).__init__() # dont understand what this does
        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  #the underscore makes this a private variable ?
    def stopped(self):
        return self._stop == True


if __name__ == "__main__":
    s = spin()
    s.cursor_invisible()
    s.start()
    a = raw_input("")
    s.stop()
    s.cursor_visible()

这篇关于将类中的微调框类型对象实现为子过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆