How to write a class that waits for N put() before yielding a result

18 views
Skip to first unread message

Luca de Alfaro

unread,
Apr 23, 2025, 10:02:00 PMApr 23
to python-simpy
Hi All, 

I am fairly new to simpy, very new in fact, and I wonder if I can get help on the following problem.  I need to write a class with two methods, put() and read(). 

An object of the class is created with something like: 

a = Average(4) 

Meaning that it waits for 4 numbers and then returns their average. 
The numbers are put in (by another process) via a.put(4), a.put(7), etc. 

The process that created the a object then does something like: 

 avg = yield a.read()

and, once 4 put() have occurred, avg will receive the average of their arguments. 

What's a good way to implement the Average class? 

I apologize if this is a simple question; I am still wrapping my mind around simpy. 
Many thanks! 

Luca
Reply all
Reply to author
Forward
0 new messages