我正在使用加載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