gpt4 book ai didi

python - 如何在类中使用 Python 中的多重处理

转载 作者:行者123 更新时间:2023-11-30 22:48:40 25 4
gpt4 key购买 nike

我想做的是在我的类方法之一上使用多重处理。我尝试按照 Python 帮助文件中的示例进行操作,但没有得到预期的结果。这是我的类文件:

import os
import telnetlib

class PowerSupply():

# ---------------------------------- #
def __init__(self,port_no,my_name):
self.Status = "Off"
self.Port = port_no
self.Name = my_name
self.tn = None
self.HOST = "192"
self.P = Process(target=self.print_time, args=('tname','delay')
self.P.start()
self.P.join()
# ---------------------------------- #
def TurnOn(self):
onCommand = "OUT 1\r"

if self.Status == "ON":
print "I'm already on"
else:
self.tn = telnetlib.Telnet(self.HOST,self.Port)
self.tn.write(onCommand)
self.Status = "ON"
print "I am now on"
# ---------------------------------- #
def TurnOff(self):
offCommand = "OUT 0\r"
self.tn.write(offCommand)
self.tn.close()
print "I am now off"
# ---------------------------------- #
def SetVoltage(self,volts):
voltageCommand = "PV" + " " + str(volts) + "\r"
self.tn.write(voltageCommand)
# ---------------------------------- #
def GetAllData(self):
while(self.Status == "ON"):
self.tn.write("DVC?\r")
all_data = self.tn.read_some()
vdc = all_data.split(',')
vdc = vdc[0]
print vdc
# ---------------------------------- #
def print_time(self, tname, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s"%(tname, time.ctime(time.time()))

以下是我尝试使用该实现的方法:

ps1 = PowerSuppy(8000,'L1')
ps1.print_time('thread1',2)
ps1.print_time('thread2',3)

当我尝试像上面那样使用它时,它仍然使用过程方法,并且在线程 1 完成之前不会调用线程 2。我到底做错了什么以及如何解决它?

最佳答案

好的,我认为该程序将执行以下操作:

  1. PowerSuppy(8000,'L1') 处,它启动一个子进程并调用 self.print_time('tname','delay')。由于 'delay' 不是一个数字,它会立即在子进程中引发 TypeError 并结束(因此 self.P.join() 没有'根本不阻止)。
  2. ps1.print_time('thread1',2) 处,它运行主进程中的方法,并被阻塞直至结束。
  3. ps1.print_time('thread2',3) 与上一行在主进程中执行的操作相同。

如何解决:

  1. 不要在__init__方法中初始化子进程,而是在print_time方法中初始化它。
  2. 为子流程的target函数实现了内部方法。
  3. 除非您想按顺序运行子流程,否则请勿运行 Process.join

代码如下:

import os
import telnetlib

class PowerSupply():

# ---------------------------------- #
def __init__(self,port_no,my_name):
self.Status = "Off"
self.Port = port_no
self.Name = my_name
self.tn = None
self.HOST = "192"

# ---------------------------------- #
def TurnOn(self):
onCommand = "OUT 1\r"

if self.Status == "ON":
print "I'm already on"
else:
self.tn = telnetlib.Telnet(self.HOST,self.Port)
self.tn.write(onCommand)
self.Status = "ON"
print "I am now on"
# ---------------------------------- #
def TurnOff(self):
offCommand = "OUT 0\r"
self.tn.write(offCommand)
self.tn.close()
print "I am now off"
# ---------------------------------- #
def SetVoltage(self,volts):
voltageCommand = "PV" + " " + str(volts) + "\r"
self.tn.write(voltageCommand)
# ---------------------------------- #
def GetAllData(self):
while(self.Status == "ON"):
self.tn.write("DVC?\r")
all_data = self.tn.read_some()
vdc = all_data.split(',')
vdc = vdc[0]
print vdc
# ---------------------------------- #
def print_time(self, tname, delay):
P = Process(target=self._print_time, args=(tname, delay))
P.start()

def _print_time(tname, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s"%(tname, time.ctime(time.time()))

关于python - 如何在类中使用 Python 中的多重处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40097485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com