use*_*459 2 python pid makefile
我正在尝试编写一个makefile来复制我编写的客户端/服务器程序(实际上只是两个Python脚本,但这不是真正令人关注的问题)......
test:
python server.py 7040 &
python subscriber.py localhost 7040 &
python client.py localhost 7040;
Run Code Online (Sandbox Code Playgroud)
所以我跑了 make test
我能够从client.py以下位置输入消息:
python server.py 7040 &
python subscriber.py localhost 7040 &
python client.py localhost 7040;
Enter a message:
Run Code Online (Sandbox Code Playgroud)
当client进入一个空的消息,他将关闭连接并成功退出.现在,我如何自动subscriber关闭聊天室(谁只是一个"倾听者") - 这将反过来退出server流程.
我试图从这些调用中获取进程ID pidof- 但是不确定这是否是正确的路由.我不是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)
Run Code Online (Sandbox Code Playgroud)
但是,现在我得到的错误是我的全局变量没有被定义(我认为它直接与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)
Run Code Online (Sandbox Code Playgroud)
在脚本中使用普通的旧bash,获取PID并使用kill.
或者,更好的是,创建一个测试脚本来处理所有这些并从Makefile中调用它.比如说一个run_tests.py.
您希望在Makefile之外保留尽可能多的逻辑.