我试图模拟一个缓冲区有限的队列,其中没有丢弃数据包但是仍在等待。因为我只是一名具有基本编码技能的学生,所以请耐心等待。
数据包以指数方式分布,每个数据包保持一个平均1250字节的数据包大小。我设法让代码工作在数据包到达+处理时间但我不能让数据包离开'并模拟队列(到目前为止,它是无限缓冲区)我有什么办法可以模拟数据包离开和队列限制吗?
代码:
import random
import simpy
RANDOM_SEED = 42
NEW_CUSTOMERS = 100 # Total number of customers
INTERVAL_CUSTOMERS = 1 # Generate new customers roughly every x seconds
SIZE = 1250
def source(env, number, interval, port):
"""Source generates packet randomly"""
for i in range(number):
size = int(random.expovariate(0.0008))
packet = Packet(env, '%d' % i, size, port, time_in_port=1)
env.process(packet)
t = random.expovariate(1 / interval)
yield env.timeout(t)
def Packet(env, id, size, port, time_in_port):
arrive = env.now
yield Queue.buffer.put(size)
print('packet%s %s arriving at %lf' % (id, size, arrive))
with port.request() as req:
yield req
tip = random.expovariate(1/time_in_port)
yield env.timeout(tip)
amount = size
yield Queue.buffer.get(amount)
print('packet%s %s finished processing at %lf' % (id, size, env.now))
class queue: #THIS PART WON'T WORK
def __init__(self, env):
self.port = simpy.Resource(env, capacity=1)
self.buffer = simpy.Container(env, init = 0, capacity=12500)
self.mon_proc = env.process(self.monitor_tank(env))
def monitor_tank(self, env):
while True:
if self.buffer.level > 12500:
print('Full at %d' % env.now)
random.seed(RANDOM_SEED)
env = simpy.Environment()
Queue = queue(env)
port = simpy.Resource(env, capacity=1)
env.process(source(env, NEW_CUSTOMERS, INTERVAL_CUSTOMERS, port))
env.run()
队列类没有用(该程序根本没有运行)。如果只删除队列类并模拟数据包到达和处理时间,它将运行。非常感谢帮助我模拟数据包离开(使用接收器)和队列限制。感谢。
答案 0 :(得分:0)
不熟悉详细信息,但您在self.monitor_tank(env)
构造函数中对queue
的调用将进入一个紧密的无限循环 - 它不是一个生成器,只是一个无休止的循环,所以python将在执行过程中陷入困境。
答案 1 :(得分:0)
您好,我认为代码将解决您的问题或至少给您一个方向。在您的原始代码中,所有包的大小都相同,我在此包中建模,但更改为字节很简单。
我使用了一个缓冲区(容器)和一个服务器(资源)。
;)
import simpy
import random
def arrival(env, buffer):
#Arrival of the Package
while True:
print('Package ARRIVED at %.1f \n\t Buffer: %i'
% (env.now, buffer.level))
yield buffer.put(1) # Put the package in the buffer
yield env.timeout(random.expovariate(1.0)) # time between arrivals
env.process(processDeparture(env, buffer, server))
def processDeparture(env, buffer, server):
#Processing and Departure of the Package
while True:
# request a Server to process thge package
request = server.request()
yield request
yield buffer.get(1) # GET a package from the buffer
# Processing time of the package
processingTime = 2
print('Package begin processing at %.1f'
% (env.now))
yield env.timeout(processingTime)
print('Package end processing at %.1f'
% (env.now))
# release the server
yield server.release(request)
random.seed(150)
env = simpy.Environment()
buffer = simpy.Container(env, capacity=3, init=0) # Create the Buffer
server = simpy.Resource(env, capacity=1) # Create the servers (resources)
env.process(arrival(env, buffer))
env.run(until=30) # Execute the Model
答案 2 :(得分:0)
我认为您代码中的这段代码是一个无限循环,并且正在阻止您的代码运行
function aws_set_language_cookie() {
//To save the language in a cookie
$current_language = get_locale();
if ($current_language == "es_ES") {
$result = setcookie('lang', $current_language, time() + (30 * DAY_IN_SECONDS), '/');
} else {
$result = setcookie('lang', $current_language, time() + (30 * DAY_IN_SECONDS), '/');
}
//To read the cookie and change the language based on the cookie
if (isset($_COOKIE['lang']) && $_COOKIE['lang'] == "es_ES") {
//need code to change website language to spanish here
}
}
尝试注释掉这篇文章,或者添加一个 env.timeout 让它在每次循环时“休眠”一段时间