Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何暂停线程直到收到Python中的数据_Python_Sockets_Networking - Fatal编程技术网

如何暂停线程直到收到Python中的数据

如何暂停线程直到收到Python中的数据,python,sockets,networking,Python,Sockets,Networking,我对线程(以及一般的网络)没有太多经验。我正在创建一个从客户端接收数据的脚本(1、2或3)。所有这些都有一个含义: 1 = NEW Apache Benchmark ITERATION - we must run new top command and append every second 2 = END Apache Benchmark ITERATION - we must end top command 3 = STOP ENTIRE PROGRAM Linux

我对线程(以及一般的网络)没有太多经验。我正在创建一个从客户端接收数据的脚本(
1
2
3
)。所有这些都有一个含义:

1   =   NEW Apache Benchmark ITERATION - we must run new top command and append every second
2   =   END Apache Benchmark ITERATION - we must end top command
3   =   STOP ENTIRE PROGRAM
Linux上的top命令只记录CPU和内存使用情况

我已经创建了一个初始线程,它侦听来自客户端的数据,并以
get\u data()
函数为目标

run()
函数等待从
get_data()
返回数据,但如果没有获得任何数据,则
run()
get_data()
函数都将停止

是否有方法暂停针对
get_data
函数的线程,直到从客户端发送数据,以便
run()
函数不会停止

我当前的代码:

import socket
import sys
import threading
import subprocess
import sched
import time
import os
import signal

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server_socket = ('xxx.xxx.x.xx', 5000)
sock.bind(server_socket)


process = None

def run_command(i):
    global process
    print("Running top")
    process = subprocess.Popen('top -b -n 1 | head -n 5 >> htop-' + str(i)  + '.txt', shell=True)
    print("Finished running top")

    return process

def get_data():
    while True:
        # data = ''
        data, address = sock.recvfrom(1024)
        print("waiting?")

        data = data.split(",")
        iteration = data[1]
        data = data[0]

        print("Data: " + data + " and iteration: " + iteration)

        time.sleep(1.0)

        # run(data, iteration)
        return data, iteration

'''
    1   =   NEW AB ITERATION - we must run new top command and append every second
    2   =   END OF AB - we must end top command
    3   =   STOP ENTIRE PROGRAM
'''

def run():
    while True:
        print("New")
        data = get_data()
        # data = data.split(",")
        iteration = data[1]
        data = data[0]

        print("Data: " + data + " and iteration: " + iteration)

        if data == '1' or data == '':
            run_command(iteration)
            print("We ran the command")

            time.sleep(1.0)

            print("Terminating")
            process.kill()
            print("We terminated the process")

        if data == '2':
            print("We got 2")
            process.kill()

        if data == '3':
            print("Closing program")
            exit()


runThread = threading.Thread(target = run)
runThread.start()


run()

print("Starting the listening thread...")

您需要使套接字非阻塞(请参阅),并重写run()过程,以处理套接字中的数据尚未收到时的情况