Skip to content

Commit 3bfa219

Browse files
committed
refactorized
1 parent 51eef05 commit 3bfa219

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

parallelqueue/components.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def Job(doPrint, queuesOverTime, replicaDict, env, name, arrive, queues, choice,
3333
try:
3434
with queues[choice].request() as request:
3535
# Wait in queue
36-
Rename = f"{name}: {choice}"
36+
Rename = f"{name}@{choice}"
3737
yield request
3838
wait = env.now - arrive
3939
# at server
@@ -82,7 +82,7 @@ def ExpJob(doPrint, meanServer, queuesOverTime, replicaDict, env, name, arrive,
8282
try:
8383
with queues[choice].request() as request:
8484
# Wait in queue
85-
Rename = f"{name}: {choice}"
85+
Rename = f"{name}@{choice}"
8686
yield request
8787
wait = env.now - arrive
8888
# at server
@@ -122,15 +122,15 @@ def PoissonArrivals(system, env, number, interval, queues, **kwargs):
122122
print("WARNING: kwargs given are ignored in the current context!")
123123
if not system.infiniteJobs:
124124
for i in range(number):
125-
c = JobRouter(system, env, 'JobRouter%02d' % i, queues)
125+
c = JobRouter(system, env, 'Job%02d' % i, queues)
126126
env.process(c)
127127
t = random.expovariate(1.0 / interval)
128128
yield env.timeout(t)
129129
else:
130130
while True: # referring to until not being passed
131131
i = number
132132
number += 1
133-
c = JobRouter(system, env, 'JobRouter%02d' % i, queues)
133+
c = JobRouter(system, env, 'Job%02d' % i, queues)
134134
env.process(c)
135135
t = random.expovariate(1.0 / interval)
136136
yield env.timeout(t)
@@ -147,16 +147,14 @@ def JobRouter(system, env, name, queues, **kwargs):
147147
:type name: str
148148
:param queues: A list of queues to consider.
149149
:type queues: List[simpy.Resource]
150-
:param jobProcess: Process defining job/replica behaviour following routing.
151-
:type jobProcess: Generator
152150
"""
153151
arrive = env.now
154152
Q_length = {i: system.NoInSystem(queues[i]) for i in system.QueueSelector(system.d, queues)}
155153
system.queuesOverTime.append({i: len(queues[i].put_queue) for i in range(len(queues))})
156154
choices = []
157-
if system.r:
158-
if system.ReplicaDict: # Replication chosen
159-
for i, value in Q_length.items():
155+
if system.ReplicaDict is not None: # Replication chosen
156+
if system.r:
157+
for i, value in Q_length.items():
160158
if value <= system.r:
161159
choices.append(i) # the chosen queue numberJobs
162160
else:
@@ -168,13 +166,15 @@ def JobRouter(system, env, name, queues, **kwargs):
168166
print(f'{arrive:7.4f} {name}: Arrival for {len(choices)} copies')
169167
replicas = []
170168
for choice in choices:
171-
c = Job(system.doPrint, system.queuesOverTime, system.ReplicaDict, env, name, arrive, queues, choice, **kwargs)
169+
c = Job(system.doPrint, system.queuesOverTime, system.ReplicaDict, env, name, arrive, queues, choice,
170+
**kwargs)
172171

173172
replicas.append(env.process(c))
174173
system.ReplicaDict[name] = replicas # Add a while statement?
175174
yield from replicas
176175
else: # Shortest queue case
177-
print(f'{arrive:7.4f} {name}: Arrival')
176+
if system.doPrint:
177+
print(f'{arrive:7.4f} {name}: Arrival')
178178
for i in Q_length:
179179
choices.append(i)
180180
choice = min(choices)
@@ -190,24 +190,20 @@ def GeneralArrivals(system, env, number, queues, **kwargs):
190190
:type env: simpy.Environment
191191
:param number: Max numberJobs of jobs if infiniteJobs is false.
192192
:type number: int
193-
:param distribution: Continuous random variable defining interarrival times.
194-
:type distribution: Union[int,float]
195193
:param queues: A list of all queues making up the parallel system.
196194
:type queues: List[simpy.Resource]
197-
:param jobProcess: Job generator
198-
:type jobProcess: generator
199195
"""
200196
if not system.infiniteJobs:
201197
for i in range(number):
202-
c = JobRouter(system, env, 'JobRouter%02d' % i, queues, **kwargs)
198+
c = JobRouter(system, env, 'Job%02d' % i, queues, **kwargs)
203199
env.process(c)
204200
t = kwargs["Arrival"](kwargs["AArgs"])
205201
yield env.timeout(t)
206202
else:
207203
while True: # referring to until not being passed
208204
i = number
209205
number += 1
210-
c = JobRouter(system, env, 'JobRouter%02d' % i, queues, **kwargs)
206+
c = JobRouter(system, env, 'Job%02d' % i, queues, **kwargs)
211207
env.process(c)
212208
t = kwargs["Arrival"](kwargs["AArgs"])
213209
yield env.timeout(t)

0 commit comments

Comments
 (0)