Python 线程 - 创建子类? [英] Python Threading - Creation of a subclass?

查看:50
本文介绍了Python 线程 - 创建子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 中使用线程时,我在围绕创建子类的原因时遇到了问题.我已经阅读了许多网站,包括 tutorialspoint.

I am having a problem wrapping my brain around the reason for creating a subclass when using threading in python. I've read a number of websites including tutorialspoint.

文档说您需要定义 Thread 类的新子类.我对类有基本的了解,但根本没有玩过子类.我还没有对我使用过的任何其他模块(如 os & )做这样的事情.ftplib.任何人都可以给我指出一个可以为新手脚本编写者更好地解释这一点的网站吗?

The docs say you need to define a new subclass of the Thread class. I have a basic understanding of classes but haven't played with subclasses at all. I haven't had to do anything like this yet with any other modules I've used like os & ftplib. Can anyone point me to a site that may explain this better for a newbie scripter?

#!/usr/bin/python

import threading

class myThread (threading.Thread):

我能够在不创建此子类的情况下编写自己的脚本并且它可以工作,所以我不确定为什么将其声明为要求.这是我创建的简单小脚本,用于帮助我最初理解线程.

I was able to write my own script without creating this subclass and it works so I am not sure why this is stated as a requirement. This is my simple little script I created to help me understand threading initially.

#!/usr/bin/python

# Import the necessary modules
import threading
import ftplib

# FTP function - Connects and performs directory listing
class
def ftpconnect(target):
        ftp = ftplib.FTP(target)
        ftp.login()
        print "File list from: %s" % target
        files = ftp.dir()
        print files

# Main function - Iterates through a lists of FTP sites creating theads
def main():
    sites = ["ftp.openbsd.org","ftp.ucsb.edu","ubuntu.osuosl.org"]
    for i in sites:
        myThread = threading.Thread(target=ftpconnect(i))
        myThread.start()
        print "The thread's ID is : " + str(myThread.ident)

if (__name__ == "__main__"):
    main()

感谢您的帮助!

我使用 tutorialspoint.com 作为我的参考资料.听起来你说我咬得比我能咀嚼的要多,考虑到我还不需要使用更复杂的选项,此时我应该保持简单.网站上是这样说的:

I am using tutorialspoint.com for my reference material. It sounds like your saying I am biting off more than I can chew and I should keep it simple at this point considering I don't need to use the more complicated options yet. This is what the site says:

Creating Thread using Threading Module:
To implement a new thread using the threading module, you have to do the following:

- Define a new subclass of the Thread class.

- Override the __init__(self [,args]) method to add additional arguments.

- Then, override the run(self [,args]) method to implement what the thread should do when started.

Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start(), which will in turn call run() method.

推荐答案

文档说您需要定义 Thread 类的新子类.

The docs say you need to define a new subclass of the Thread class.

我能够在不创建此子类的情况下编写自己的脚本并且它可以工作,所以我不确定为什么将其声明为要求.

I was able to write my own script without creating this subclass and it works so I am not sure why this is stated as a requirement.

Python 文档没有说这样的话,并且无法猜测您在谈论哪些文档.以下是 Python 文档:

The Python docs say no such thing, and can't guess which docs you're talking about. Here are Python docs:

有两种方法可以指定活动:通过将可调用对象传递给构造函数,或者通过覆盖子类中的 run() 方法.不应在子类中覆盖其他方法(构造函数除外).换句话说,只覆盖该类的 init() 和 run() 方法.

There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, only override the init() and run() methods of this class.

您正在使用那里指定的第一个方法(将可调用对象传递给 Thread() 构造函数).没关系.当可调用对象需要访问状态变量并且您不希望为此目的使用全局变量使程序混乱时,子类变得更有价值,尤其是在使用多个线程时,每个线程都需要自己的状态变量.然后状态变量通常可以在您自己的 threading.Thread 子类上实现为实例变量.如果您(暂时)不需要它,请不要担心(暂时).

You're using the first method specified there (passing a callable to the Thread() constructor). That's fine. Subclasses become more valuable when the callable needs access to state variables and you don't want to clutter your program with globals for that purpose, and especially when using multiple threads that each need their own state variables. Then state variables can usually be implemented as instance variables on your own subclass of threading.Thread. If you don't need that (yet), don't worry about it (yet).

这篇关于Python 线程 - 创建子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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