Python 用于循环线程mininet popen
在我的Python程序中,使用mininet,我有一个for循环,其中每个客户机运行一个Java程序,其中的输出是一个时间(整数),由Python脚本在写入文本文件的Python 用于循环线程mininet popen,python,multithreading,python-multithreading,mininet,Python,Multithreading,Python Multithreading,Mininet,在我的Python程序中,使用mininet,我有一个for循环,其中每个客户机运行一个Java程序,其中的输出是一个时间(整数),由Python脚本在写入文本文件的popen中捕获 但是,每个客户端只运行一次,需要重新连接到服务器程序才能获得另一次。其工作方式是让每个客户端持续连接并从服务器接收一段时间,直到程序完成 在client for循环中添加while True循环将不起作用,因为脚本的其余部分在手动关闭之前永远不会完成 那么,如何使每个客户端都可以使用以下方式连续连接到服务器: ar
popen
中捕获
但是,每个客户端只运行一次,需要重新连接到服务器程序才能获得另一次。其工作方式是让每个客户端持续连接并从服务器接收一段时间,直到程序完成
在client for循环中添加while True
循环将不起作用,因为脚本的其余部分在手动关闭之前永远不会完成
那么,如何使每个客户端都可以使用以下方式连续连接到服务器:
args = 'java -jar client.jar ' + str(server[0].IP()) + ' ' + server[1] + ' report ' + device_id + ' ' + level
popens[client] = \
client.popen(args)
以下是我正在使用的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections, pmonitor
from mininet.log import setLogLevel
from mininet.node import Controller, OVSSwitch
#from mininet.node import OVSController
from signal import SIGINT
import math
import random
import time
import threading
def runExperiment(
policy,
port,
device_id,
level,
):
'''Run experiments with n clients and m servers'''
#controller = RemoteController()
topo = NetworkTopology()
net = Mininet(topo, switch=OVSSwitch)
net.start()
# Get hosts
hosts = net.hosts
# Separate hosts into clients and servers
clients = []
servers = {}
i = 0
for host in hosts:
# If its a server add to server list
# else add to client list
if 'server' in host.name:
servers[host] = str(int(port)+i)
i += 1
else:
clients.append(host)
#info("*** Testing connections\n")
#net.pingAll()
popens = {}
for server in servers:
popens[server] = \
server.popen('java -jar server.jar ' + port + ' ' + policy)
time.sleep(15) # Wait 10 seconds for servers to start
for client in clients:
# Get random server from servers list
server = random.choice(servers.items())
args = 'java -jar client.jar ' + str(server[0].IP()) + ' ' + server[1] + ' report ' + device_id + ' ' + level
popens[client] = \
client.popen(args)
endTime = time.time() + 10 # Run for 60 seconds
for (h, line) in pmonitor(popens):#, timeoutms=500):
if h:
with open('times.txt', 'a') as myfile:
myfile.write(line)
if time.time() >= endTime:
for p in popens.values():
p.send_signal(SIGINT)
net.stop()
# network with n hosts connected to one switch
class NetworkTopology(Topo):
clients = 10
servers = 2
def __init__(self):
# Initialize topology
Topo.__init__(self)
controller = self.addController('c0')
switch = self.addSwitch('s1')
for s in range(self.servers):
server = self.addHost('server%s' % (s + 1))
self.addLink(server, switch)
for c in range(self.clients):
client = self.addHost('client%s' % (c + 1))
self.addLink(client, switch)
topos = {'mytopo': lambda : NetworkTopology()}
if __name__ == '__main__':
setLogLevel('info')
policy = 'RBIBA'
port = '6540'
device_id = 'ec5ae996-2b1f-4f85-a168-b3f8e2abf897'
level = '2'
runExperiment(policy, port, device_id, level)
注意:我尝试了以下方法,但没有收到任何结果:
def thread_client(popens, client, args):
popens[client] = \
client.popen(args)
for client in clients:
# Get random server from servers list
server = random.choice(servers.items())
args = 'java -jar client.jar ' + str(server[0].IP()) + ' ' + server[1] + ' report ' + device_id + ' ' + level
c = threading.Thread(target=thread_client, args=(popens, client, args,))
c.start()
当程序完成时,您对
是什么意思?什么时候应该发生这种情况?也就是说,当所有代码都已运行完时--runexperience(params)
已完成runexperience(params)
已完成。但您对它在完成之前需要做什么的想法与实现不同(目前大致为每台服务器运行一次客户机)。您如何知道要完成的全部工作范围?如何在变量中跟踪它?回答这个问题后,您可以编写一个while循环,该循环执行while(condition),直到完整的计算完成。为了澄清需要发生什么,编写一个完全按顺序进行的版本可能更简单,并且根本不使用mininet。一旦澄清了这一点,就可以使用mininet引入并行执行。@AdamBurke目标是同时运行多个客户端(当java程序的一个执行完成时,启动另一个),而不管可能有多少服务器。