我正在使用加载Cygwin的香草Python 2.7我想要能够产生调用顶级函数的线程子类,并且顶级函数会生成单独的线程来调用子级函数。下面是伪代码如何在python中的同一对象中的另一个线程内产生一个线程?
import threading
#!/usr/bin/python
import threading
class Server(threading.Thread):
def __init__(self, threadID, target):
self.__threadID = threadID
self.__target = target
threading.Thread.__init__(self)
# Function called when the thread's start() function is called
def run(self):
self.target()
pass
# This is the top level function called by other objects
def reboot(self):
# I want this function to spawn two threads
# - First thread calls the __powerDown() function
# - Secod thread calls the __powerUp() function, and pends
# until __powerDown() thread finishes
pass
def __powerDown(self):
# What to put here?
pass
def __powerUp(self):
# What to put here?
pass
__threadID = ''
__target = None
# Code calling above code
server = Server(123, reboot) # Will this work?
其实我的代码有一个错误(缺少'target'关键字)。请现在查看代码,因为您的编辑不正确(它正在调用函数而不是将它传递给线程)。 – freakish