threading.Thread(target=check).start()
时间: 2023-08-15 12:03:09 浏览: 38
这是一个使用 threading 模块创建新线程并启动的代码。具体来说,它创建了一个名为 check 的函数的新线程,并且在后台运行该函数。可以将 check 函数定义为需要在后台运行的任何类型的操作,例如监视系统状态或访问网络资源。通过将该函数作为参数传递给 threading.Thread() 并调用 start() 方法,可以启动新线程并开始执行该函数。
相关问题
我的python代码中创建了两个线程,其中当check_system_run_status函数的某个计算值超过规定阈值,check_collect_count_down函数需要执行函数中的某一个分支,两个函数写在不同的py文件里,且由第三个py文件启动这两个线程, 我应该如何实现这一功能 checkTrainTask = threading.Thread(target=c_settings.check_collect_count_down) checkTrainTask.start() checkSystemStatus = threading.Thread(target=c_system.check_system_run_status) checkSystemStatus.start()
要实现这一功能,你可以使用共享变量或队列来在两个线程之间传递信息。以下是一个示例,演示了如何在两个线程中传递信息并根据条件执行相应的分支:
**文件1: c_settings.py**
```python
import time
# 共享变量
threshold = 10
def check_collect_count_down():
global threshold
while True:
# 模拟计算值的变化
threshold += 1
print("Threshold:", threshold)
time.sleep(1)
```
**文件2: c_system.py**
```python
import time
def check_system_run_status():
global threshold
while True:
if threshold > 20:
print("Threshold exceeded!")
# 执行分支A的代码
else:
print("Threshold not exceeded!")
# 执行分支B的代码
time.sleep(1)
```
**文件3: main.py**
```python
import threading
import c_settings
import c_system
def start_threads():
checkTrainTask = threading.Thread(target=c_settings.check_collect_count_down)
checkTrainTask.start()
checkSystemStatus = threading.Thread(target=c_system.check_system_run_status)
checkSystemStatus.start()
# 启动线程
start_threads()
```
在上述示例中,我们在`c_settings.py`中定义了一个全局变量`threshold`,并在`check_collect_count_down`函数中对其进行递增操作。在`c_system.py`中,我们在`check_system_run_status`函数中检查`threshold`的值,并根据条件执行相应的分支。
通过使用共享变量`threshold`,两个线程可以共享这个值并进行相应的判断。当`threshold`超过阈值时,第二个线程将执行分支A的代码;否则,将执行分支B的代码。
确保在代码中正确导入和使用函数,并根据你的需求进行适当的修改。
检查代码中的错误 class ADB_SHELL: def get_ip(self): # 执行adb shell命令并输出结果 subprocess.check_output( ['adb', 'shell', 'udhcpc'] ) subprocess.check_output( ['adb', 'shell', 'udhcpc -i eth1'] ) self.conf = subprocess.check_output( ['adb', 'shell', 'ifconfig'] ).decode() # conf = str(ip).split(r'\r\r\n') # tmp = conf.replace( "\r\r\n", "\n" ) # print( tmp) self.ip = re.findall( r'addr:(.*?) Bcast', str( self.conf ) ) print(self.ip) for self.i in selfip : speed = subprocess.check_output((['adb', 'shell', f'iperf3 -B {self.i} -c 192.168.102.105'])).decode() print(speed.replace("\r\r\n", "\n")) for i in range(5): write_data = subprocess.check_output(['adb', 'shell', 'time dd if=/dev/zero of=/data/test.data bs=128k count=1024']).decode() print(write_data.replace("\r\r\n", "\n")) read_data = subprocess.check_output(['adb', 'shell', 'time dd if=/data/test.data of=/dev/null bs=128k count=1024']).decode() print(read_data.replace("\r\r\n", "\n")) ls = subprocess.check_output( ['adb', 'shell', 'ls /data'] ).decode() print( ls.replace( '\r', ' ' ) ) dl = subprocess.check_output( ['adb', 'shell', 'rm /data/test.data'] ).decode() l = subprocess.check_output(['adb', 'shell', 'ls /data']).decode() print( l.replace( '\r', ' ' ) ) subprocess.check_output( (['adb', 'shell', f'iperf3 -s']) ) def get_ssh(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) for i in self.ip: ssh.connect(hostname='192.168.102.105',port=22,username='root',password='xiayi123456',timeout=30) stdin,stdout,stderr = ssh.exec_command(f'iperf3 -c {i}') print(stdout.read()) # ssh.close() if __name__ == '__main__': # get_ip() # get_ssh() A = ADB_SHELL t1 = threading.Thread(target=A.get_ssh) t2 = threading.Thread(target=A.get_ip) t1.start() t2.start() t1.join() t2.join()
在第12行代码中,将 `selfip` 改为 `self.ip`。此外,第28行代码中的 `ssh.set_missing_host_key_policy` 应该是 `ssh.set_missing_host_key_policy()`。最后,第39行代码中的 `A = ADB_SHELL` 应该是 `A = ADB_SHELL()`,因为 ADB_SHELL 是一个类,需要实例化才能调用其方法。