从 Makefile 中终止进程

发布于 2024-10-30 08:26:27 字数 2730 浏览 0 评论 0原文

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

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

所以我运行 make test< /code>

并且我能够从 client.py 输入消息:

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

client 输入空消息时,他会关闭连接并成功退出。现在,我如何自动关闭聊天室的订阅者(他只是一个“监听者”) - 这将依次退出服务器进程。

我试图使用 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) 

但是,现在我收到错误,表明我的全局变量未定义(我认为它与添加直接相关)我的 servermain 方法(以及 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)

I'm trying to write a makefile that will replicate a client/server program I've written (which is really just two Python scripts, but that's not the real question of concern)...

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

So I run make test

and I get the ability to enter a message from client.py:

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

When the client enters an empty message, he closes the connection and quits successfully. Now, how can I automate the subscriber (who is just a "listener) of the chat room to close - which will in turn exit the server process.

I was trying to get the process IDs from these calls using pidof - but wasn't really sure if that was the correct route. I am no makefile expert; maybe I could just write a quick Python script that gets executed from my makefile to do the work for me? Any suggestions would be great.


EDIT:

I've gone writing the Python script route, and have the following:

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) 

However, now I'm getting errors that my global variables are not defined ( I think its directly related to adding the main methods to my server (and subscriber and client, but I'm not getting that far yet:). This may deserve a separate question...

Here's my server code:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-11-06 08:26:27

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

或者,更好的是,创建一个处理所有这些的测试脚本,并从 Makefile 中调用那个。比如说,一个 run_tests.py。

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

Use plain old bash in the script, get the PID and use kill.

Or, much much much much better, create a testing script that handles all that and call that from your Makefile. A single run_tests.py, say.

You want to keep as much logic as possible outside the Makefile.

也只是曾经 2024-11-06 08:26:27

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

related to 'global' issue => define handle_client inside main and remove the global message, client_count,... line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文