开发者

Kill Process from Makefile

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('local开发者_运维百科host', 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)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜