Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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 从Makefile杀死进程_Python_Makefile_Pid - Fatal编程技术网

Python 从Makefile杀死进程

Python 从Makefile杀死进程,python,makefile,pid,Python,Makefile,Pid,我正在尝试编写一个makefile,它将复制我编写的客户机/服务器程序(实际上只是两个Python脚本,但这不是真正需要考虑的问题) 所以我运行maketest 我可以从client.py输入消息: python server.py 7040 & python subscriber.py localhost 7040 & python client.py localhost 7040; Enter a message: 当客户端输入一条空消息时,他将关闭连接并成功退出。现在,

我正在尝试编写一个makefile,它将复制我编写的客户机/服务器程序(实际上只是两个Python脚本,但这不是真正需要考虑的问题)

所以我运行
maketest

我可以从
client.py
输入消息:

python server.py 7040 & 
python subscriber.py localhost 7040 &
python client.py localhost 7040;
Enter a message:
客户端
输入一条空消息时,他将关闭连接并成功退出。现在,我如何自动关闭聊天室的
订阅者
(他只是一个“监听者”),从而退出
服务器
过程

我试图使用
pidof
从这些调用中获取进程ID,但不确定这是否是正确的路径。我不是makefile专家;也许我可以编写一个快速的Python脚本,从我的makefile中执行,为我做这项工作?任何建议都会很好


编辑

我已经开始编写Python脚本路线,并有以下内容:

import server
import client
import subscriber
#import subprocess

server.main(8092)
# child = subprocess.Popen("server.py",shell=False)
subscriber.main('localhost',8090)
client.main('localhost', 8090) 
但是,现在我遇到了一些错误,没有定义全局变量(我认为这与将
main
方法添加到
server
(以及
subscriber
client
,但我还没有做到这一点:)。这可能需要一个单独的问题

这是我的服务器代码:

import socket
import select
import sys
import thread
import time

# initialize list to track all open_sockets/connected clients
open_sockets = []   

# thread for each client that connects
def handle_client(this_client,sleeptime):
    global message,client_count,message_lock,client_count_lock 

    while 1:
        user_input = this_client.recv(100) 

        if user_input == '': 
            break

        message_lock.acquire()
        time.sleep(sleeptime)
        message += user_input
        message_lock.release()
        message = message + '\n'

        this_client.sendall(message)

    # remove 'this_client' from open_sockets list
    open_sockets.remove(this_client)
    this_client.close()

    client_count_lock.acquire()
    client_count -= 1
    client_count_lock.release()


def main(a):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    port = a
    server.bind(('', port))
    server.listen(5)
    message = ''
    message_lock = thread.allocate_lock()
    client_count = 2
    client_count_lock = thread.allocate_lock()

    for i in range(client_count):        
        (client,address) = server.accept()
        open_sockets.append(client)    
        thread.start_new_thread(handle_client,(client,2))

    server.close()

    while client_count > 0: 
        pass  

    print '************\nMessage log from all clients:\n%s\n************' % message

if __name__ == "__main__":
    if sys.argv[1]:
        main(int(sys.argv[1]))
    else:
        main(8070)

在脚本中使用普通的旧bash,获取PID并使用
kill

或者,更好的是,创建一个测试脚本来处理所有这些,并从Makefile中调用that


您希望在Makefile之外保留尽可能多的逻辑。

与“全局”问题相关=>在
main
内部定义
handle\u client
并删除
global消息、client\u count、

太棒了……我现在正在执行脚本路线。我将发布我的位置。我的服务器文件中有问题当我正常运行它时没有…说我的
global
变量没有定义:(
import socket
import select
import sys
import thread
import time

# initialize list to track all open_sockets/connected clients
open_sockets = []   

# thread for each client that connects
def handle_client(this_client,sleeptime):
    global message,client_count,message_lock,client_count_lock 

    while 1:
        user_input = this_client.recv(100) 

        if user_input == '': 
            break

        message_lock.acquire()
        time.sleep(sleeptime)
        message += user_input
        message_lock.release()
        message = message + '\n'

        this_client.sendall(message)

    # remove 'this_client' from open_sockets list
    open_sockets.remove(this_client)
    this_client.close()

    client_count_lock.acquire()
    client_count -= 1
    client_count_lock.release()


def main(a):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    port = a
    server.bind(('', port))
    server.listen(5)
    message = ''
    message_lock = thread.allocate_lock()
    client_count = 2
    client_count_lock = thread.allocate_lock()

    for i in range(client_count):        
        (client,address) = server.accept()
        open_sockets.append(client)    
        thread.start_new_thread(handle_client,(client,2))

    server.close()

    while client_count > 0: 
        pass  

    print '************\nMessage log from all clients:\n%s\n************' % message

if __name__ == "__main__":
    if sys.argv[1]:
        main(int(sys.argv[1]))
    else:
        main(8070)