Queue size appears to be the ENV size in SimPy - python

I am just starting to work on an event simulation, and I am having some issues with monitoring the queue.
It appears that everytime I check the queue, it is actually showing the Env.now. Any advice?
import simpy
num_of_machines = 2
env = simpy.Environment()
bcs = simpy.Resource(env, capacity=num_of_machines)
def monitor(resource):
"""This is our monitoring callback."""
print('Queue size: %s' % len(resource.queue))
def process_client(env, name):
with bcs.request() as req:
yield req
print('%s starting to charge at %s' % (name, env.now))
yield env.timeout(90)
print('%s ending charge at %s' % (name, env.now))
monitor(bcs)
def setup(env):
i = 0
while True:
i += 1
yield env.timeout(1)
env.process(process_client(env, ('Car %s' % i)))
env.process(setup(env))
env.run(until=300)
Results:
Car 1 starting to charge at 1
Car 2 starting to charge at 2
Car 1 ending charge at 91
Queue size: 88
Car 3 starting to charge at 91
Car 2 ending charge at 92
Queue size: 88
Car 4 starting to charge at 92
Car 3 ending charge at 181
Queue size: 176
Car 5 starting to charge at 181
Car 4 ending charge at 182
Queue size: 176
Car 6 starting to charge at 182
Car 5 ending charge at 271
Queue size: 264
Car 7 starting to charge at 271
Car 6 ending charge at 272
Queue size: 264
Car 8 starting to charge at 272

You spawn a process_client() every timestep, so when the first of these processes is done after 90 time steps, you already have created 90 new processes that are queueing up. So your numbers are looking quite right.

Related

How to combine three columns into one in python

Excel table = this is the excel file screenshot which is how final result should be. Please take closer look at "Lifestyle" section.
I can't figure out how to make my python just like the excel picture screenshot. "Lifestyle" section needs to have 2 more sub-columns combined just like in a picture below. Any help would be appreciated.
I'm gonna post picture below PyCharm screenshot:
Here is my code:
#convert inches to feet-inches
def inch_to_feet(x):
feet = x // 12
inch = x % 12
return str(feet)+"'"+str(inch)+'"'
#file opened
print("Hello")
roster = input("Please enter a roster file: ")
if roster != "roster_extended.csv":
print("Invalid name")
elif roster == "roster_extended.csv":
additional_name = input("There are 13 lines in this file. Would you like to enter an additional names? (Y/N): ")
if additional_name == "Y":
input("How many more names?: ")
infile = open("roster_extended.csv", 'r')
b = infile.readline()
b = infile.readlines()
header = '{0:>12} {1:>35} {2:>3} {3:>16} {4:>5} {5:>3} {6:>9}'.format("FirstName","LastName","Age","Occupation","Ht","Wt","lifestyle")
print(header)
with open("roster_extended.csv", "a+") as infile:
b = infile.write(input("Enter first name: "))
for person in b:
newperson = person.replace("\n", "").split(",")
newperson[4] = eval(newperson[4])
newperson[4] = inch_to_feet(newperson[4])
newperson
formatted='{0:>12} {1:>35} {2:>3} {3:>16} {4:>5} {5:>3} {6:>9}'.format(newperson[0],newperson[1],newperson[2],newperson[3],newperson[4],newperson[5],newperson[6])
print(formatted)
Here is the output I get:
FirstName LastName Age Occupation Ht Wt lifestyle
Anna Barbara 35 nurse 5'3" 129
Catherine Do 45 physicist 5'5" 135
Eric Frederick 28 teacher 5'5" 140
Gabriel Hernandez 55 surgeon 5'7" 150 x
Ivy Joo 31 engineer 5'2" 126 x
Kelly Marks 21 student 5'4" 132
Nancy Owens 60 immunologist 5'8" 170 x
Patricia Qin 36 dental assistant 4'11" 110 x
Roderick Stevenson 51 bus driver 5'6" 160 x
Tracy Umfreville 42 audiologist 5'7" 156 x
Victoria Wolfeschlegelsteinhausenbergerdorff 38 data analyst 5'8" 158
Lucy Xi 49 professor 5'9" 161
Yolanda Zachary 58 secretary 5'10" 164 x
Brief explanation of the solution:
You gave tabulated data as input (there are several ways to tabulate: check here). Since you're starting with python the solution keeps within standard library (thus not resorting to external libraries). Only format() and class variables are used to keep track of column width (if you delete elements you'll want to update the variables.) This programmatically automates tabulation.
Since you are starting out, I recommend putting a breakpoint in __init__() and __new__() to observe their behavior.
I used Enum because conceptually it's the right tool for the job. You only need to understand Enum.name and Enum.value, as for everything else consider it a normal class.
There are 2 output files, one in tabulated form and the other in barebone csv.
(For the most part the solution is "canonical" (or close). The procedural part was rushed, but gives a sufficient idea.)
import csv
import codecs
from enum import Enum
from pathlib import Path
IN_FILE = Path("C:\\your_path\\input.csv")
OUT_FILE = Path("C:\\your_path\\output1.csv")
OUT_FILE_TABULATE = Path("C:\\your_path\\output2.csv")
def read_csv(file) -> list:
with open(file) as csv_file:
reader_csv = csv.reader(csv_file, delimiter=',')
for row in reader_csv:
yield row
def write_file(file, result_ordered):
with codecs.open(file, "w+", encoding="utf-8") as file_out:
for s in result_ordered:
file_out.write(s + '\n')
class LifeStyle(Enum):
Sedentary = 1
Active = 2
Moderate = 3
def to_list(self):
list_life_style = list()
for one_style in LifeStyle:
if one_style is self:
list_life_style.append('x')
else:
list_life_style.append('')
return list_life_style
def tabulate(self):
str_list_life_style = list()
for one_style in LifeStyle:
if one_style is not self:
str_list_life_style.append('{: ^{width}}'.format(' ', width=len(one_style.name)))
else:
str_list_life_style.append('{: ^{width}}'.format('x', width=len(self.name)))
return str_list_life_style
def tabulate_single_column(self):
return '{: >{width}}'.format(str(self.name), width=len(LifeStyle.Sedentary.name))
#staticmethod
def header_single_column():
return ' {}'.format(LifeStyle.__name__)
#staticmethod
def header():
return ' {} {} {}'.format(
LifeStyle.Sedentary.name,
LifeStyle.Active.name,
LifeStyle.Moderate.name,
)
class Person:
_FIRST_NAME = "First Name"
_LAST_NAME = "Last Name"
_AGE = "Age"
_OCCUPATION = "Occupation"
_HEIGHT = "Height"
_WEIGHT = "Weight"
max_len_first_name = len(_FIRST_NAME)
max_len_last_name = len(_LAST_NAME)
max_len_occupation = len(_OCCUPATION)
def __new__(cls, first_name, last_name, age, occupation, height, weight, lifestyle):
cls.max_len_first_name = max(cls.max_len_first_name, len(first_name))
cls.max_len_last_name = max(cls.max_len_last_name, len(last_name))
cls.max_len_occupation = max(cls.max_len_occupation, len(occupation))
return super().__new__(cls)
def __init__(self, first_name, last_name, age, occupation, height, weight, lifestyle):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.occupation = occupation
self.height = height
self.weight = weight
self.lifestyle = lifestyle
#classmethod
def _tabulate_(cls, first_name, last_name, age, occupation, height, weight):
first_part = '{: >{m_first}} {: >{m_last}} {: >{m_age}} {: <{m_occup}} {: <{m_height}} {: >{m_weight}}'.format(
first_name,
last_name,
age,
occupation,
height,
weight,
m_first=Person.max_len_first_name,
m_last=Person.max_len_last_name,
m_occup=Person.max_len_occupation,
m_age=len(Person._AGE),
m_height=len(Person._HEIGHT),
m_weight=len(Person._WEIGHT))
return first_part
#classmethod
def header(cls, header_life_style):
first_part = Person._tabulate_(Person._FIRST_NAME, Person._LAST_NAME, Person._AGE, Person._OCCUPATION,
Person._HEIGHT, Person._WEIGHT)
return '{}{}'.format(first_part, header_life_style)
def __str__(self):
first_part = Person._tabulate_(self.first_name, self.last_name, self.age, self.occupation, self.height,
self.weight)
return '{}{}'.format(first_part, ' '.join(self.lifestyle.tabulate()))
def single_column(self):
first_part = Person._tabulate_(self.first_name, self.last_name, self.age, self.occupation, self.height,
self.weight)
return '{} {}'.format(first_part, self.lifestyle.tabulate_single_column())
def populate(persons_populate):
for line in read_csv(IN_FILE):
life_style = ''
if line[6] == 'x':
life_style = LifeStyle.Sedentary
elif line[7] == 'x':
life_style = LifeStyle.Moderate
elif line[8] == 'x':
life_style = LifeStyle.Active
persons_populate.append(Person(line[0], line[1], line[2], line[3], line[4], line[5], life_style))
return persons_populate
persons = populate(list())
print(Person.header(LifeStyle.header()))
for person in persons:
print(person)
write_file(OUT_FILE_TABULATE, [str(item) for item in persons])
# add new persons here
persons.append(Person("teste", "teste", "22", "worker", "5'8\"", "110", LifeStyle.Active))
final_list = list()
for person in persons:
one_list = [person.first_name, person.last_name, person.age, person.occupation, person.height,
person.weight]
one_list.extend([item.strip() for item in person.lifestyle.tabulate()])
final_list.append(','.join(one_list))
write_file(OUT_FILE, final_list)
print("\n", Person.header(LifeStyle.header_single_column()))
for person in persons:
print(person.single_column())
output1.csv:
Anna,Barbara,35,nurse,5'3",129,,,x
Catherine,Do,45,physicist,5'5",135,,x,
Eric,Frederick,28,teacher,5'5",140,,,x
Gabriel,Hernandez,55,surgeon,5'7",150,x,,
Ivy,Joo,31,engineer,5'2",126,x,,
Kelly,Marks,21,student,5'4",132,,x,
Nancy,Owens,60,immunologist,5'8",170,x,,
Patricia,Qin,36,dental assistant,4'11",110,x,,
Roderick,Stevenson,51,bus driver,5'6",160,x,,
Tracy,Umfreville,42,audiologist,5'7",156,x,,
Victoria,Wolfeschlegelsteinhausenbergerdorff,38,data analyst ,5'8",158,,,x
Lucy,Xi,49,professor,5'9",161,,,x
Yolanda,Zachary,58,secretary,5'10",164,x,,
teste,teste,22,worker,5'8",110,,x,
output2.csv:
Anna Barbara 35 nurse 5'3" 129 x
Catherine Do 45 physicist 5'5" 135 x
Eric Frederick 28 teacher 5'5" 140 x
Gabriel Hernandez 55 surgeon 5'7" 150 x
Ivy Joo 31 engineer 5'2" 126 x
Kelly Marks 21 student 5'4" 132 x
Nancy Owens 60 immunologist 5'8" 170 x
Patricia Qin 36 dental assistant 4'11" 110 x
Roderick Stevenson 51 bus driver 5'6" 160 x
Tracy Umfreville 42 audiologist 5'7" 156 x
Victoria Wolfeschlegelsteinhausenbergerdorff 38 data analyst 5'8" 158 x
Lucy Xi 49 professor 5'9" 161 x
Yolanda Zachary 58 secretary 5'10" 164 x
single_column:
Anna Barbara 35 nurse 5'3" 129 Moderate
Catherine Do 45 physicist 5'5" 135 Active
Eric Frederick 28 teacher 5'5" 140 Moderate
Gabriel Hernandez 55 surgeon 5'7" 150 Sedentary
Ivy Joo 31 engineer 5'2" 126 Sedentary
Kelly Marks 21 student 5'4" 132 Active
Nancy Owens 60 immunologist 5'8" 170 Sedentary
Patricia Qin 36 dental assistant 4'11" 110 Sedentary
Roderick Stevenson 51 bus driver 5'6" 160 Sedentary
Tracy Umfreville 42 audiologist 5'7" 156 Sedentary
Victoria Wolfeschlegelsteinhausenbergerdorff 38 data analyst 5'8" 158 Moderate
Lucy Xi 49 professor 5'9" 161 Moderate
Yolanda Zachary 58 secretary 5'10" 164 Sedentary
teste teste 22 worker 5'8" 110 Active

ValueError: ParentedTree.read(): expected 'end-of-string' but got '(:'

Here is a step-by-step code to debug the error I'm getting:
from nltk.tree import ParentedTree
teststr = 'a) Any service and handling fee imposed on customers by Used Motor Vehicle Dealers subject to these Rules: \r\n 1) shall be charged uniformly to all retail customers; \r\n 2) may not be presented as mandatory in writing, electronically, verbally, via American Sign Language, or via other media as mandatory; nor presented as mandatory or mandated by any entity, other than the Arkansas Used Motor Vehicle Dealer who or dealership which is legally permitted to invoice, charge and collect the service and handling fee established by these Rules; \r\n 3) must follow the procedures for disclosure set out by these Rules.'
#Using ALLENNLP's parser
from allennlp.predictors.predictor import Predictor
conspredictor = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/elmo-constituency-parser-2018.03.14.tar.gz")
treestr = conspredictor.predict(sentence=teststr)['trees']
ptree = ParentedTree.fromstring(treestr)
Here is the error I'm receiving with the traceback:
<ipython-input-391-f600cbe3ff5e> in <module>
10 treestr = conspredictor.predict(sentence=teststr)['trees']
11
---> 12 ptree = ParentedTree.fromstring(treestr)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\tree.py in fromstring(cls, s, brackets, read_node, read_leaf, node_pattern, leaf_pattern, remove_empty_top_bracketing)
616 if token[0] == open_b:
617 if len(stack) == 1 and len(stack[0][1]) > 0:
--> 618 cls._parse_error(s, match, 'end-of-string')
619 label = token[1:].lstrip()
620 if read_node is not None: label = read_node(label)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\tree.py in _parse_error(cls, s, match, expecting)
677 offset = 13
678 msg += '\n%s"%s"\n%s^' % (' '*16, s, ' '*(17+offset))
--> 679 raise ValueError(msg)
680
681 #////////////////////////////////////////////////////////////
ValueError: ParentedTree.read(): expected 'end-of-string' but got '(:'
at index 273.
"...es))))))) (: :) (S (..."
^

Python script to inject bulks into KairosDB - only 1st bulk inserted, the rest ignored

I wrote the following script that allows me to set the amount of sensors and the amount of bulk inserts for them.
Each bulk insert gets a different epoch time.
Each sensor insert gets a different value + different tag (temp) value.
Kairos version:
root#ip-172-16-0-147:~# echo "version" | nc -w 30 localhost 4242
KairosDB 1.2.0-0.3beta.20171211170411
Scylla (Backend Storage) version:
[centos#ip-172-16-0-128 ~]$ scylla --version
2.0.2-0.20171201.07b039f
The problem:
Only the 1st bulk is inserted into Kairos, the other bulks are just ignored and I can't figure out why.
Any ideas as to why, or how to correct the script are appreciated.
The script:
import socket
import time
import random
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("[Kairos_IP]", 4242))
start_time = time.time()
lowerRange = 1
upperRange = 5
sensor_amount = 10
sensors = []
for x in range(lowerRange, upperRange):
str=""
curr_epoch = time.time()
#curr_epoch = int(time.time() * 1000)
time.sleep(1)
for y in range(1,sensor_amount):
statement = "put SENSOR_%d %d %d temp=%d\n" %(y,curr_epoch,random.randint(1,500),random.randint(1,500))
sensors.append(statement)
str+=(sensors[y-1])
print statement
s.send(str)
print curr_epoch
print("--- %s seconds ---" % (time.time() - start_time))
Example of output:
put SENSOR_1 1515672684 240 temp=500
put SENSOR_2 1515672684 279 temp=403
put SENSOR_3 1515672684 380 temp=376
put SENSOR_4 1515672684 59 temp=38
put SENSOR_5 1515672684 405 temp=46
put SENSOR_6 1515672684 61 temp=255
put SENSOR_7 1515672684 19 temp=21
put SENSOR_8 1515672684 246 temp=80
put SENSOR_9 1515672684 289 temp=233
1515672684.72
put SENSOR_1 1515672685 58 temp=12
put SENSOR_2 1515672685 245 temp=199
put SENSOR_3 1515672685 492 temp=220
put SENSOR_4 1515672685 185 temp=432
put SENSOR_5 1515672685 235 temp=416
put SENSOR_6 1515672685 218 temp=297
put SENSOR_7 1515672685 378 temp=3
put SENSOR_8 1515672685 317 temp=397
put SENSOR_9 1515672685 103 temp=229
1515672685.72
put SENSOR_1 1515672686 482 temp=395
put SENSOR_2 1515672686 417 temp=7
put SENSOR_3 1515672686 285 temp=111
put SENSOR_4 1515672686 67 temp=322
put SENSOR_5 1515672686 402 temp=3
put SENSOR_6 1515672686 120 temp=410
put SENSOR_7 1515672686 232 temp=50
put SENSOR_8 1515672686 90 temp=263
put SENSOR_9 1515672686 315 temp=48
1515672686.72
put SENSOR_1 1515672687 181 temp=453
put SENSOR_2 1515672687 212 temp=414
put SENSOR_3 1515672687 116 temp=138
put SENSOR_4 1515672687 106 temp=118
put SENSOR_5 1515672687 92 temp=348
put SENSOR_6 1515672687 178 temp=361
put SENSOR_7 1515672687 148 temp=198
put SENSOR_8 1515672687 334 temp=79
put SENSOR_9 1515672687 191 temp=192
1515672687.72
Found the problem - here is the correct version of the script:
import socket
import time
import random
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("[Kairos_IP]", 4242))
start_time = time.time()
sensor_amount = 501
#sensors = []
#curr_epoch = int(round(time.time() * 1000))
for num_repeat in range(1,501):
str=""
time.sleep(2)
curr_epoch = int(round(time.time() * 1000))
print "Current time " + time.strftime("%X")
print "======================"
for y in range(1,sensor_amount):
statement = "putm SENSOR_%d %d %d temp=%d\n" %(y,curr_epoch,random.randint(1,500),random.randint(1,500))
print statement
s.send(statement)
print("--- %s seconds ---" % (time.time() - start_time))

hadoop mapreduce with python error in implement of two table join

I'm new to hadoop. I use python to implement a join of two table.
A table with student info as stu id and name and another table with grade info as stu id, class id and grade.(The data is in the end of this question)
with cat ./data* | python mapper.py |sort| python reducer.py
I get the right answer, however in the hadoop process, I get the following error:
INFO mapreduce.Job: Task Id : attempt_1456069308240_0019_m_000001_2, Status : FAILED
Error: java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed with code 2
the code is below:
mapper code:
import os
import sys
def mapper():
for line in sys.stdin:
if len(line.strip())== 0:
continue
field = line[:-1].split('\t')
if len(field) ==2:
print '%s\t%s\t%s' % (field[0],'0',field[1])
elif len(field) ==3:
print '%s\t%s\t%s\t%s' % (field[0],'1',field[1],field[2])
if __name__=='__main__':
mapper()
reducer code:
def reducer():
lastsno=""
for line in sys.stdin:
if len(line.strip())== 0:
continue
field = line[:-1].split('\t')
sno = field[0]
if sno != lastsno:
name = ""
if field[1] == "0":
name = field[2]
elif sno == lastsno:
if field[1] == "1":
courseno =field[2]
grade = field[3]
if name:
print '%s\t%s\t%s\t%s' % (lastsno,name,courseno,grade)
lastsno = sno
if __name__=='__main__':
reducer()
the data is just below
datainfo:
2014211011 taiyuan
2014211012 tiantian
2014211013 kk
2014211014 tank
2014211015 lc
2014211016 wjs
2014211017 gyc
2014211018 hd
2014211019 cx
data_grade:
2014211012 011 80
2014211012 012 95
2014211013 011 40
2014211014 013 38
2014211011 011 35
2014211013 022 40
2014211015 011 80
with above cat python script, I can get right answer below:
2014211011 taiyuan 011 35
2014211012 tiantian 011 80
2014211012 tiantian 012 95
2014211013 kk 011 40
2014211013 kk 022 40
2014211014 tank 013 38
2014211015 lc 011 80
But error in hadoop, please help me, Thanks.

python Queue multithreading

What is the simplest way to have only two trains(in the same time) on the railroad. My english is bad. this is only way how I can explain it. I know I should use Queue? I can't find info in my language
Thank you!
1>go, 2>go. 3,4wait. 1>finish, 3>go (4th still wait) ..
from threading import Thread
import time
import random
def trains(city):
print city, 'start'
for count in range(1,3):
delay = random.randrange(5,10)
print city, 'delay', delay
time.sleep(delay)
print city, 'end'
cities = ['prague', 'london', 'berlin', 'moscow']
threadlist = []
for city in cities:
t = Thread(target=trains, args=(city,))
t.start()
threadlist.append(t)
for b in threadlist:
b.join()
I'm going to guess at some things here:
from threading import Thread, Lock, BoundedSemaphore
import time
import random
def trains(city):
with railroads:
with iolock:
print city, 'start'
for count in range(1,3):
delay = random.randrange(5,10)
with iolock:
print city, 'delay', delay
time.sleep(delay)
with iolock:
print city, 'end'
cities = ['prague', 'london', 'berlin', 'moscow']
threadlist = []
iolock = Lock()
railroads = BoundedSemaphore(2)
for city in cities:
t = Thread(target=trains, args=(city,))
t.start()
threadlist.append(t)
for b in threadlist:
b.join()
The purpose of iolock is to stop mixed-up output in your terminal: only 1 thread prints at a time. The purpose of railroads is to allow at most two threads to enter the body of the code simultaneously. Here's sample output. Note that "prague" and "london" happen to run at first, but "berlin" doesn't start before "london" ends. Then "moscow" doesn't start until "prague" ends:
prague start
london start
prague delay 8
london delay 5
london delay 6
prague delay 5
london end
berlin start
berlin delay 8
prague end
moscow start
moscow delay 8
berlin delay 6
moscow delay 7
berlin end
moscow end

Categories