【问题标题】:Kill Process from Makefile从 Makefile 杀死进程
【发布时间】:2011-04-04 21:43:29
【问题描述】:

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

test:
    python server.py 7040 & 
    python subscriber.py localhost 7040 &
    python client.py localhost 7040;

所以我运行make test

我可以输入来自client.py的消息:

python server.py 7040 & 
python subscriber.py localhost 7040 &
python client.py localhost 7040;
Enter a message:

client输入空消息时,他关闭连接并成功退出。现在,我怎样才能自动关闭聊天室的subscriber(他只是一个“听众)”——这又会退出server 进程。

我试图使用 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 (以及subscriberclient)直接相关,但我还没有那么远:)。这可能值得一个单独的问题...

这是我的服务器代码:

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)

【问题讨论】:

    标签: python makefile pid


    【解决方案1】:

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

    或者,要好得多,创建一个测试脚本来处理所有这些并从您的 Makefile 调用 that。比如说,一个 run_tests.py。

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

    【讨论】:

    • 太棒了...我现在正在沿着脚本路线前进。我会在我所在的地方发布。我的服务器文件出现问题,当我正常运行它时没有这些问题......说我的 global 变量未定义:(
    【解决方案2】:

    与“全局”问题相关 => 在 main 中定义 handle_client 并删除 global message, client_count,...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 2012-01-31
      相关资源
      最近更新 更多