File: Manual.html

package info (click to toggle)
python-simpy 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 17,300 kB
  • ctags: 2,568
  • sloc: python: 11,172; makefile: 145
file content (3966 lines) | stat: -rw-r--r-- 304,949 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta content="SimPy Python Simulation Language" name="description" />
<meta content="simulation python stochastic" name="keywords" />

    <title>SimPy Manual &mdash; SimPy 2.3.1 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.3.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="SimPy 2.3.1 documentation" href="../index.html" />
    <link rel="up" title="Manuals" href="../Manuals.html" />
    <link rel="next" title="SimPy’s Object Oriented API" href="SimPyOO_API.html" />
    <link rel="prev" title="Manuals" href="../Manuals.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="SimPyOO_API.html" title="SimPy’s Object Oriented API"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../Manuals.html" title="Manuals"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">SimPy 2.3.1 documentation</a> &raquo;</li>
          <li><a href="../Manuals.html" accesskey="U">Manuals</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="simpy-manual">
<h1><a class="toc-backref" href="#id15">SimPy Manual</a><a class="headerlink" href="#simpy-manual" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Authors:</th><td class="field-body"><ul class="first simple">
<li>Tony Vignaux &lt;<a class="reference external" href="mailto:Vignaux&#37;&#52;&#48;users&#46;sourceforge&#46;net">Vignaux<span>&#64;</span>users<span>&#46;</span>sourceforge<span>&#46;</span>net</a>&gt;</li>
<li>Klaus Muller &lt;<a class="reference external" href="mailto:Muller&#37;&#52;&#48;users&#46;sourceforge&#46;net">Muller<span>&#64;</span>users<span>&#46;</span>sourceforge<span>&#46;</span>net</a>&gt;</li>
<li>Bob Helmbold</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">SimPy release:</th><td class="field-body"><p class="first">2.3.1</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">SimPy Web-site:</th><td class="field-body"><p class="first"><a class="reference external" href="http://simpy.sourceforge.net/">http://simpy.sourceforge.net/</a></p>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Python-Version:</th><td class="field-body"><p class="first">2.6 and later</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Date:</th><td class="field-body"><p class="first last">January 28, 2012</p>
</td>
</tr>
</tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#simpy-manual" id="id15">SimPy Manual</a><ul>
<li><a class="reference internal" href="#introduction" id="id16">Introduction</a></li>
<li><a class="reference internal" href="#simulation-with-simpy" id="id17">Simulation with SimPy</a></li>
<li><a class="reference internal" href="#processes" id="id18">Processes</a></li>
<li><a class="reference internal" href="#resources" id="id19">Resources</a></li>
<li><a class="reference internal" href="#levels" id="id20">Levels</a></li>
<li><a class="reference internal" href="#stores" id="id21">Stores</a></li>
<li><a class="reference internal" href="#random-number-generation" id="id22">Random Number Generation</a></li>
<li><a class="reference internal" href="#recording-simulation-results" id="id23">Recording Simulation Results</a></li>
<li><a class="reference internal" href="#other-links" id="id24">Other Links</a></li>
<li><a class="reference internal" href="#acknowledgments" id="id25">Acknowledgments</a></li>
<li><a class="reference internal" href="#appendices" id="id26">Appendices</a></li>
<li><a class="reference internal" href="#glossary" id="id27">Glossary</a></li>
</ul>
</li>
</ul>
</div>
<p>This document describes SimPy version 2.3.1.  Changes from the previous
version are listed in <a class="reference internal" href="#a0-changes-from-the-previous-version-of-simpy">Appendix A0</a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This document does <strong>not</strong> describe the object oriented (OO) API
which has been added to SimPy with version 2.0. SimPy 2.0
is fully backward compatible with previous versions. The
procedural API and the OO API co-exist happily in SimPy 2.x.</p>
</div>
<table class="docutils footnote" frame="void" id="id1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[1]</a></td><td>The variable <tt class="docutils literal"><span class="pre">version</span></tt>, imported from <tt class="docutils literal"><span class="pre">SimPy.Simulation</span></tt>,
contains the revision number and date of the current version.</td></tr>
</tbody>
</table>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id16">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>SimPy is a Python-based discrete-event simulation system that models
active components such as messages, customers, trucks, planes by
parallel processes. It provides a number of tools for the simulation
programmer including <a class="reference internal" href="#processes">Processes</a> to model active entities, three kinds
of resource facilities (<a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a>, and <a class="reference internal" href="#stores">Stores</a>) and ways of
recording results by using <a class="reference internal" href="#defining-tallys-and-monitors">Monitors</a> and <a class="reference internal" href="#defining-tallys-and-monitors">Tallys</a>.</p>
<p>The basic active elements of a SimPy model are process objects (i.e.,
objects of a Process class &#8211; see <a class="reference internal" href="#processes">Processes</a>).  As a general practice
and for brevity we will often refer to both process objects and their
classes as &#8220;processes.&#8221;  Thus, &#8220;process&#8221; may refer to a Process class
or to a process object, depending on context. To avoid ambiguity or
for added emphasis we often explicitly state whether a class or an
object is intended. In addition we will use &#8220;entity&#8221; to refer to
process objects as this is frequently used in the simulation
literature. Here, though, we restrict it to process objects and it
will not be used for any other elements in the simulation.</p>
<p>During the simulation, Process objects may be delayed for fixed or
random times, queued at resource facilities, and may be interrupted by
or interact in other ways with other processes and components. For
example, Automobiles in a model of a gas station may have to queue
while waiting for a pump to become available . Once obtaining a pump it
takes some time to fill before releasing the pump.</p>
<p>A SimPy script contains the declaration of one or more Process classes
and the creation of process objects (entities) from them.  Each
process object executes its <em>Process Execution Method</em> (referred to
later as a <a class="reference internal" href="#pem">PEM</a>), a method that determines its actions. Each PEM runs
in parallel with (and may interact with) the PEMs of other process
objects.</p>
<p>There are three types of resource facilities (<a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a>, and
<a class="reference internal" href="#stores">Stores</a>). Each type models a congestion point where process objects
may have to queue while waiting to acquire or, in some cases to
deposit, a resource.</p>
<p id="index-0"><a class="reference internal" href="#resources">Resources</a> have several <em>resource units</em>, each of which may be used by
process objects. Extending the example above, the gas station might be
modelled as a resource with its pumps as resource units. On receiving
a request for a pump from a car, the gas station resource
automatically queues waiting cars until one becomes available. The
pump resource unit is held by the car until it is released for
possible use by another car.</p>
<p id="index-1"><a class="reference internal" href="#levels">Levels</a> model the supply and consumption of a homogeneous
undifferentiated &#8220;material.&#8221; The Level at any time holds an amount of
material that is fully described by a scalar (real or integer). This
can be increased or decreased by process objects. For example, a gas
(petrol) station stores gas in large storage tanks. The tanks can be
increased by Tanker deliveries and reduced by cars refuelling. A car
need not return the gas to the Level in contrast to the requirement
for Resource units.</p>
<p id="index-2"><a class="reference internal" href="#stores">Stores</a> model the production and consumption of individual items. A
store hold a list of items.  Process objects can insert or remove
items from the list.  For example, surgical procedures (treated as
process objects) require specific lists of personnel and equipment
that may be treated as the items in a Store facility such as a
clinic or hospital. The items held in a Store can be of any Python
type. In particular they can be process objects, and this may be
exploited to facilitate modelling Master/Slave relationships.</p>
<p>In addition to the number of free units or quantities, resource
facilities all hold queues of waiting process objects which are
operated automatically by SimPy. They also operate a reneging
mechanism so that a process object can abandon the wait.</p>
<p id="index-3"><a class="reference internal" href="#defining-tallys-and-monitors">Monitors</a> and <a class="reference internal" href="#defining-tallys-and-monitors">Tallys</a> are used to compile statistics as a function of
time on variables such as waiting times and queue lengths. These
statistics consist of simple averages and variances, time-weighted
averages, or histograms. They can be gathered on the queues associated
with Resources, Levels and Stores. For example we may collect
data on the average number of cars waiting at a gas station and the
distribution of their waiting times. Tallys update the current
statistics as the simulation progresses, but cannot preserve complete
time-series records. Monitors can preserve complete time-series
records that may later be used for more advanced post-simulation
analyses.</p>
<p>Before attempting to use SimPy, you should be able to write Python
code. In particular, you should be able to define and use classes and
their objects. Python is free and usable on most platforms.  We do not
expound it here. You can find out more about it and download it from
the <a class="reference external" href="http://www.Python.org">Python</a> web-site (<a class="reference external" href="http://www.Python.org">http://www.Python.org</a>). SimPy requires <em>Python</em>
2.3 or later.</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="simulation-with-simpy">
<h2><a class="toc-backref" href="#id17">Simulation with SimPy</a><a class="headerlink" href="#simulation-with-simpy" title="Permalink to this headline">¶</a></h2>
<p>To use the SimPy simulation system you must import its <tt class="docutils literal"><span class="pre">Simulation</span></tt>
module (or one of the <a class="reference internal" href="#alternative-simpy-simulation-libraries">alternatives</a>):</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">from</span> <span class="pre">SimPy.Simulation</span> <span class="pre">import</span> <span class="pre">*</span></tt></div></blockquote>
<p>All discrete-event simulation programs automatically maintain the
current simulation time in a software clock. This cannot be changed by
the user directly.  In SimPy the current clock value is returned by the
<tt class="docutils literal"><span class="pre">now()</span></tt> function.</p>
<p>At the start of the simulation the software clock is set to 0.0.
While the simulation program runs, simulation time steps forward from
one <em>event</em> to the next. An event occurs whenever the state of the
simulated system changes. For example, an event might be the arrival
or departure of a car from the gas station.</p>
<p>The following statement initializes global simulation variables and
sets the software clock to zero. It must appear in the script before
any SimPy process objects are activated.</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">initialize(</span> <span class="pre">)</span></tt></div></blockquote>
<p>This is followed by SimPy statements creating and activating process
objects. Activation of process objects adds events to the simulation
schedule. Execution of the simulation itself starts with the following
statement:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">simulate(until=</span></tt><em>endtime</em><tt class="docutils literal"><span class="pre">)</span></tt></div></blockquote>
<p>The simulation starts, and SimPy seeks and executes the first
scheduled event. Having executed that event, the simulation seeks and
executes the next event, and so on.</p>
<p>Typically a simulation is terminated when <em>endtime</em> is reached but it
can be  stopped at any time by the command:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">stopSimulation(</span> <span class="pre">)</span></tt></div></blockquote>
<p><tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)</span></tt> will then equal the time when this was called. The
simulation will also stop if there are no more events to execute (so
<tt class="docutils literal"><span class="pre">now()</span></tt> equals the time the last scheduled event occurred)</p>
<p>After the simulation has stopped, further statements can be executed.
<tt class="docutils literal"><span class="pre">now()</span></tt> will retain the time of stopping and data held in Monitors
will be available for display or further analysis.</p>
<p>The following fragment shows only the <em>main</em> block in a simulation
program.  (Complete, runnable examples are shown in <a class="reference internal" href="#example-1">Example 1</a> and
<a class="reference internal" href="#example-2">Example 2</a>). Here <tt class="docutils literal"><span class="pre">Message</span></tt> is a (previously defined) Process
class and <tt class="docutils literal"><span class="pre">m</span></tt> is defined as an object of that class, that is, a
particular message. Activating <tt class="docutils literal"><span class="pre">m</span></tt> has the effect of scheduling at
least one event by starting the PEM of <tt class="docutils literal"><span class="pre">m</span></tt> (here called <tt class="docutils literal"><span class="pre">go</span></tt>).
The <tt class="docutils literal"><span class="pre">simulate(until=1000.0)</span></tt> statement starts the simulation itself,
which immediately jumps to the first scheduled event. It will continue
until it runs out of events to execute or the simulation time reaches
<tt class="docutils literal"><span class="pre">1000.0</span></tt>. When the simulation stops the (previously written) <tt class="docutils literal"><span class="pre">Report</span></tt>
function is called to display the results:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span class="n">initialize</span><span class="p">()</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Message</span><span class="p">()</span>
<span class="n">activate</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">m</span><span class="o">.</span><span class="n">go</span><span class="p">(),</span><span class="n">at</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">)</span>

<span class="n">Report</span><span class="p">()</span>  <span class="c">#  report results when the simulation finishes</span>
</pre></div>
</td></tr></table></div>
<div class="section" id="the-object-oriented-interface">
<span id="index-4"></span><h3>The object-oriented interface<a class="headerlink" href="#the-object-oriented-interface" title="Permalink to this headline">¶</a></h3>
<p>An object-oriented API interface was added in SimPy 2.0.  It is
described more fully in <tt class="docutils literal"><span class="pre">SimPyOO_API</span></tt>. It defines a class of
<em>Simulation</em> objects and makes running multiple simulations cleaner
and easier. It is compatible with the procedural version described in
this Manual. Using the object-oriented API, the program fragment listed
at the end of the previous subsection would look like this:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre><span class="n">s</span><span class="o">=</span><span class="n">Simulation</span><span class="p">()</span>
<span class="n">s</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">sim</span><span class="o">=</span><span class="n">s</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">m</span><span class="p">,</span><span class="n">m</span><span class="o">.</span><span class="n">go</span><span class="p">(),</span><span class="n">at</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mf">1000.0</span><span class="p">)</span>

<span class="n">Report</span><span class="p">()</span>  <span class="c">#  report results when the simulation finishes</span>
</pre></div>
</td></tr></table></div>
<p>Further examples of the OO style exist in the <em>SimPyModels</em> directory
and the <em>Bank Tutorial</em>.</p>
</div>
<div class="section" id="alternative-simpy-simulation-libraries">
<h3>Alternative SimPy simulation libraries<a class="headerlink" href="#alternative-simpy-simulation-libraries" title="Permalink to this headline">¶</a></h3>
<p>In addition to <em>SimPy.Simulation</em>, SimPy provides four alternative
simulation libraries which have the basic <tt class="docutils literal"><span class="pre">SimPy.Simulation</span></tt>
capabilities, plus additional facilities:</p>
<ul class="simple">
<li><em>SimPy.SimulationTrace</em> for program tracing:
With <tt class="docutils literal"><span class="pre">from</span> <span class="pre">SimPy.SimulationTrace</span> <span class="pre">import</span> <span class="pre">*</span></tt>, any SimPy program automatically
generates detailed event-by-event tracing output. This makes the library
ideal for program development/testing and for teaching SimPy.</li>
<li><em>SimPy.SimulationRT</em> for real time synchronization:
<tt class="docutils literal"><span class="pre">from</span> <span class="pre">SimPy.SimulationRT</span> <span class="pre">import</span> <span class="pre">*</span></tt> facilitates synchronizing simulation
time and real (wall-clock) time. This capability can be used to implement,
e.g., interactive game applications or to demonstrate a model&#8217;s execution
in real time.</li>
<li><em>SimPy.SimulationStep</em> for event-stepping through a simulation:
The import <tt class="docutils literal"><span class="pre">from</span> <span class="pre">SimPy.SimulationStep</span> <span class="pre">import</span> <span class="pre">*</span></tt> provides an API for
stepping through a simulation event by event. This can assist with
debugging models, interacting with them on an event-by-event basis,
getting event-by-event output from a model (e.g. for plotting purposes),
etc.</li>
<li><em>SimPy.SimulationGUIDebug</em> for event-stepping through a simulation
with a GUI: <tt class="docutils literal"><span class="pre">from</span> <span class="pre">SimPy.SimulationGUIDebug</span> <span class="pre">import</span> <span class="pre">*</span></tt> provides an
API for stepping through a simulation event-by-event, with a GUI
for user control. The event list, Process and Resource objects are
shown in windows. This is useful for debugging models and for teaching
discrete event simulation with SimPy.</li>
</ul>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
<div class="section" id="processes">
<span id="index-5"></span><h2><a class="toc-backref" href="#id18">Processes</a><a class="headerlink" href="#processes" title="Permalink to this headline">¶</a></h2>
<p>The active objects for discrete-event simulation in SimPy are process
objects &#8211; instances of some class that inherits from SimPy&#8217;s Process
class.</p>
<p>For example, if we are simulating a computing network we might model
each message as an object of the class <tt class="docutils literal"><span class="pre">Message</span></tt>.  When message
objects arrive at the computing network they make transitions between
nodes, wait for service at each one, are served for some time, and
eventually leave the system. The <tt class="docutils literal"><span class="pre">Message</span></tt> class specifies all the
actions of each message in its Process Execution Method (PEM).
Individual message objects are created as the simulation runs, and
their evolutions are directed by the <tt class="docutils literal"><span class="pre">Message</span></tt> class&#8217;s PEM.</p>
<div class="section" id="defining-a-process">
<span id="index-6"></span><h3>Defining a process<a class="headerlink" href="#defining-a-process" title="Permalink to this headline">¶</a></h3>
<p>Each Process class inherits from SimPy&#8217;s <tt class="docutils literal"><span class="pre">Process</span></tt> class. For example
the header of the definition of a new <tt class="docutils literal"><span class="pre">Message</span></tt> Process class would
be:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">class</span> <span class="pre">Message(Process):</span></tt></div></blockquote>
<p id="index-7">At least one Process Execution Method (PEM) must be defined in each
Process class <a class="footnote-reference" href="#id1" id="id2">[1]</a>.  A PEM may have arguments in addition to the required
<tt class="docutils literal"><span class="pre">self</span></tt> argument that all methods must have. Naturally, other
methods and, in particular, an <tt class="docutils literal"><span class="pre">__init__</span></tt> method, may be defined.</p>
<table class="docutils footnote" frame="void" id="id3" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4">[2]</a></td><td>More than one can be defined but only one can be executed by
any process object.</td></tr>
</tbody>
</table>
<ul id="pem">
<li><p class="first"><tt class="docutils literal"><span class="pre">A</span> <span class="pre">Process</span> <span class="pre">Execution</span> <span class="pre">Method</span> <span class="pre">(PEM)</span></tt> defines the actions that are
performed by its process objects. Each PEM must contain at least one
of the <tt class="docutils literal"><span class="pre">yield</span></tt> statements, described later. This makes it a Python
generator function so that it has resumable execution &#8211; it can be
restarted again after the yield statement without losing its current
state.  A PEM may have any name of your choice. For example it may
be called <tt class="docutils literal"><span class="pre">execute(</span> <span class="pre">)</span></tt> or <tt class="docutils literal"><span class="pre">run(</span> <span class="pre">)</span></tt>.</p>
<p id="index-8">&#8220;The <tt class="docutils literal"><span class="pre">yield</span></tt> statements are simulation commands which affect an
ongoing life-cycle of Process objects. These statements control the
execution and synchronization of multiple processes. They can delay
a process, put it to sleep, request a shared resource or provide a
resource. They can add new events on the simulation event schedule,
cancel existing ones, or cause processes to wait for a state
change.&#8221;</p>
<p>For example, here is a the Process Execution Method, <tt class="docutils literal"><span class="pre">go(self)</span></tt>,
for the <tt class="docutils literal"><span class="pre">Message</span></tt> class. Upon activation it prints out the current
time, the message object&#8217;s identification number and the word
&#8220;Starting&#8221;. After a simulated delay of 100.0 time units (in the
<tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold,</span> <span class="pre">...</span></tt> statement) it announces that this message object
has &#8220;Arrived&#8221;:</p>
<div class="highlight-python"><pre>def go(self):
    print now(), self.i, 'Starting'
    yield hold,self,100.0
    print now(), self.i, 'Arrived'</pre>
</div>
<p>A process object&#8217;s PEM starts execution when the object is
activated, provided the <tt class="docutils literal"><span class="pre">simulate(until=</span> <span class="pre">...)</span></tt> statement has been
executed.</p>
</li>
<li><p class="first"><strong>__init__(self, ...)</strong>, where <em>...</em> indicates method
arguments. This method initializes the process object, setting
values for some or all of its attributes.  As for any sub-class in
Python, the first line of this method must call the <tt class="docutils literal"><span class="pre">Process</span></tt> class&#8217;s
<tt class="docutils literal"><span class="pre">__init__(</span> <span class="pre">)</span></tt> method in the form:</p>
<blockquote>
<div><p><tt class="docutils literal"><span class="pre">Process.__init__(self)</span></tt></p>
</div></blockquote>
<p>You can then use additional commands to initialize attributes of the
Process class&#8217;s objects. You can also override the standard <tt class="docutils literal"><span class="pre">name</span></tt>
attribute of the object.</p>
<p>The <tt class="docutils literal"><span class="pre">__init__(</span> <span class="pre">)</span></tt> method is always called whenever you create a
new process object. If you do not wish to provide for any attributes
other than a <tt class="docutils literal"><span class="pre">name</span></tt>, the <tt class="docutils literal"><span class="pre">__init__</span></tt> method may be dispensed
with. An example of an <tt class="docutils literal"><span class="pre">__init__(</span> <span class="pre">)</span></tt> method is shown in the
example below.</p>
</li>
</ul>
</div>
<div class="section" id="creating-a-process-object">
<span id="index-9"></span><h3>Creating a process object<a class="headerlink" href="#creating-a-process-object" title="Permalink to this headline">¶</a></h3>
<p>An entity (process object) is created in the usual Python manner by
calling the class. Process classes have a single argument, <tt class="docutils literal"><span class="pre">name</span></tt>
which can be specified if no <tt class="docutils literal"><span class="pre">__init__</span></tt> method is defined. It defaults to
<tt class="docutils literal"><span class="pre">'a_process'</span></tt>. It can be over-ridden if an <tt class="docutils literal"><span class="pre">__init__</span></tt> method is defined.</p>
<p>For example to create a new <tt class="docutils literal"><span class="pre">Message</span></tt> object with a name
<tt class="docutils literal"><span class="pre">Message23</span></tt>:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">m</span> <span class="pre">=</span> <span class="pre">Message(name=&quot;Message23&quot;)</span></tt></div></blockquote>
<hr class="docutils" />
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When working through this and all other SimPy manuals,
the reader is encouraged to type in, run and experiment with
all examples as she goes. No better way of learning exists
than <strong>doing</strong>! A suggestion: if you want to see how a SimPy
model is being executed, <em>trace</em> it by replacing <cite>from SimPy.Simulation
import *</cite> with <cite>from SimPy.SimulationTrace import *</cite>.
Any Python environment is suitable &#8211; an interactive Python session,
IDLE, IPython, Scite . . .</p>
</div>
<blockquote id="example-1">
<span id="index-10"></span><div><p><strong>Example 1</strong>: This is is a complete, runnable, SimPy script. We
declare a <tt class="docutils literal"><span class="pre">Message</span></tt> class and define an <tt class="docutils literal"><span class="pre">__init__(</span> <span class="pre">)</span></tt> method and
a PEM called <tt class="docutils literal"><span class="pre">go(</span> <span class="pre">)</span></tt>.  The <tt class="docutils literal"><span class="pre">__init__(</span> <span class="pre">)</span></tt> method provide an
instance variables of an identification number and message length.
We do not actually use the <tt class="docutils literal"><span class="pre">len</span></tt> attribute in this example.</p>
<p>Two <tt class="docutils literal"><span class="pre">messages</span></tt>, <tt class="docutils literal"><span class="pre">p1</span></tt> and <tt class="docutils literal"><span class="pre">p2</span></tt> are created. <tt class="docutils literal"><span class="pre">p1</span></tt> and <tt class="docutils literal"><span class="pre">p2</span></tt>
are activated to start at simulation times 0.0 and 6.0,
respectively. Nothing happens until the <tt class="docutils literal"><span class="pre">simulate(until=200)</span></tt>
statement. When both messages have finished (at time
6.0+100.0=106.0) there will be no more events so the simulation will
stop at that time:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">simulate</span>


<span class="k">class</span> <span class="nc">Message</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A simple Process&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="nb">len</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;Message&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">i</span> <span class="o">=</span> <span class="n">i</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">len</span> <span class="o">=</span> <span class="nb">len</span>

    <span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">i</span><span class="p">,</span> <span class="s">&#39;Starting&#39;</span><span class="p">))</span>
        <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mf">100.0</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">i</span><span class="p">,</span> <span class="s">&#39;Arrived&#39;</span><span class="p">))</span>


<span class="n">initialize</span><span class="p">()</span>
<span class="n">p1</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">203</span><span class="p">)</span>   <span class="c"># new message</span>
<span class="n">activate</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span> <span class="n">p1</span><span class="o">.</span><span class="n">go</span><span class="p">())</span>  <span class="c"># activate it</span>
<span class="n">p2</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">33</span><span class="p">)</span>
<span class="n">activate</span><span class="p">(</span><span class="n">p2</span><span class="p">,</span> <span class="n">p2</span><span class="o">.</span><span class="n">go</span><span class="p">(),</span> <span class="n">at</span><span class="o">=</span><span class="mf">6.0</span><span class="p">)</span>
<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;Current time is </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>  <span class="c"># will print 106.0</span>
</pre></div>
</td></tr></table></div>
<p>Running this program gives the following output:</p>
<div class="highlight-python"><pre>0 1 Starting
6.0 2 Starting
100.0 1 Arrived
106.0 2 Arrived
Current time is 106.0
</pre>
</div>
</div></blockquote>
</div>
<hr class="docutils" />
<div class="section" id="elapsing-time-in-a-process">
<h3>Elapsing time in a Process<a class="headerlink" href="#elapsing-time-in-a-process" title="Permalink to this headline">¶</a></h3>
<p>A <a class="reference internal" href="#pem">PEM</a> uses the <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> command to temporarily delay a process
object&#8217;s operations.</p>
<span class="target" id="index-11"></span><div class="section" id="yield-hold">
<span id="index-12"></span><h4>yield hold<a class="headerlink" href="#yield-hold" title="Permalink to this headline">¶</a></h4>
<blockquote>
<div><p><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold,self,</span></tt><em>t</em></p>
<p>Causes the process object to delay <em>t</em> time units <a class="footnote-reference" href="#id3" id="id4">[2]</a>.  After the
delay, it continues with the next statement in its PEM.  During
the <tt class="docutils literal"><span class="pre">hold</span></tt> the object&#8217;s operations are suspended.</p>
</div></blockquote>
<table class="docutils footnote" frame="void" id="id5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id6">[3]</a></td><td><p class="first">unless it is further delayed by being <a class="reference internal" href="#asynchronous-interruptions">interrupted</a>.</p>
<p class="last">This is used to model any elapsed time  an entity might be involved
in. For example while it is passively being provided with service.</p>
</td></tr>
</tbody>
</table>
<hr class="docutils" />
<blockquote id="example-2">
<span id="index-13"></span><div><p><strong>Example 2:</strong> In this example the Process Execution Method,
<tt class="docutils literal"><span class="pre">buy</span></tt>, has an extra argument, <tt class="docutils literal"><span class="pre">budget</span></tt>:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">simulate</span>


<span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">buy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>   <span class="n">budget</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;Here I am at the shops </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">t</span> <span class="o">=</span> <span class="mf">5.0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">t</span>
            <span class="c"># executed 4 times at intervals of t time units</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;I just bought something </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">budget</span> <span class="o">-=</span> <span class="mf">10.00</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;All I have left is </span><span class="si">%s</span><span class="s"> I am going home </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">budget</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>


<span class="n">initialize</span><span class="p">()</span>

<span class="c"># create a customer named &quot;Evelyn&quot;,</span>
<span class="n">C</span> <span class="o">=</span> <span class="n">Customer</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;Evelyn&#39;</span><span class="p">)</span>

<span class="c"># and activate her with a budget of 100</span>
<span class="n">activate</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="n">C</span><span class="o">.</span><span class="n">buy</span><span class="p">(</span><span class="n">budget</span><span class="o">=</span><span class="mi">100</span><span class="p">),</span> <span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>

<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mf">100.0</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div></blockquote>
<hr class="docutils" />
</div>
</div>
<div class="section" id="starting-and-stopping-simpy-process-objects">
<h3>Starting and stopping SimPy Process Objects<a class="headerlink" href="#starting-and-stopping-simpy-process-objects" title="Permalink to this headline">¶</a></h3>
<p>A process object is &#8220;passive&#8221; when first created, i.e., it has no
scheduled events. It must be <em>activated</em> to start its Process
Execution Method. To activate an instance of a Process class you can
use either the <tt class="docutils literal"><span class="pre">activate</span></tt> function or the <tt class="docutils literal"><span class="pre">start</span></tt> method of the
Process. (see the <a class="reference internal" href="#glossary">Glossary</a> for an explanation of the modified Backus-Naur Form (BNF)
notation used).</p>
<div class="section" id="activate">
<span id="index-14"></span><h4>activate<a class="headerlink" href="#activate" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">activate(</span></tt><em>p, p.pemname([args])</em><tt class="docutils literal"><span class="pre">[,{at=now()|delay=0}][,prior=False])</span></tt></p>
<p>activates process object <em>p</em>, provides its Process Execution Method
<em>p.pemname( )</em> with arguments <em>args</em> and possibly assigns values to the
other optional parameters. The default is to activate at the current
time (<tt class="docutils literal"><span class="pre">at=now(</span> <span class="pre">)</span></tt>) with no delay (<tt class="docutils literal"><span class="pre">delay=0.0</span></tt>) and <tt class="docutils literal"><span class="pre">prior</span></tt> set to
<tt class="docutils literal"><span class="pre">False</span></tt>.  You may assign other values to <tt class="docutils literal"><span class="pre">at</span></tt>, <tt class="docutils literal"><span class="pre">delay</span></tt>, and <tt class="docutils literal"><span class="pre">prior</span></tt>.</p>
<p>Example: to activate a process object, <tt class="docutils literal"><span class="pre">cust</span></tt> with name
<tt class="docutils literal"><span class="pre">cust001</span></tt> at time 10.0 using a PEM called <tt class="docutils literal"><span class="pre">lifetime</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">activate</span><span class="p">(</span><span class="n">cust</span><span class="p">,</span><span class="n">cust</span><span class="o">.</span><span class="n">lifetime</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;cust001&#39;</span><span class="p">),</span><span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
</pre></div>
</div>
<p>However, <tt class="docutils literal"><span class="pre">delay</span></tt> overrides <tt class="docutils literal"><span class="pre">at</span></tt>, in the sense that when a
<tt class="docutils literal"><span class="pre">delay=</span></tt><em>period</em> clause is included, then activation occurs at
<tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)</span></tt> or <tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)+</span></tt><em>period</em> (whichever is larger), irrespective of
what value of <em>t</em> is assigned in the <tt class="docutils literal"><span class="pre">at=</span></tt><em>t</em> clause. This is true
even when the value of <em>period</em> in the delay clause is zero, or
even negative.  So it is better and clearer to choose one (or
neither) of <tt class="docutils literal"><span class="pre">at=</span></tt><em>t</em> and <tt class="docutils literal"><span class="pre">delay=</span></tt><em>period</em>, but not both.</p>
<p>If you set <tt class="docutils literal"><span class="pre">prior=True</span></tt>, then process object <em>p</em> will be
activated <em>before</em> any others that happen to be scheduled for
activation at the same time. So, if several process objects are
scheduled for activation at the same time and all have
<tt class="docutils literal"><span class="pre">prior=True</span></tt>, then the last one scheduled will actually be the
first to be activated, the next-to-last of those scheduled, the
second to be activated, and so forth.</p>
<p>Retroactive activations that attempt to activate a process
object before the current simulation time terminate the
simulation with an error report.</p>
</li>
</ul>
</div>
<div class="section" id="start">
<span id="index-15"></span><h4>start<a class="headerlink" href="#start" title="Permalink to this headline">¶</a></h4>
<p>An alternative to <tt class="docutils literal"><span class="pre">activate()</span></tt> function is the <tt class="docutils literal"><span class="pre">start</span></tt>
method. There are a number of ways of using it:</p>
<ul>
<li><p class="first"><em>p.</em><tt class="docutils literal"><span class="pre">start(</span></tt><em>p.pemname([args])</em><tt class="docutils literal"><span class="pre">[,{at=now()|delay=0}][,prior=False])</span></tt></p>
<p>is an alternative to the <tt class="docutils literal"><span class="pre">activate</span></tt> statement. <em>p</em> is a Process
object.  The generator function, <em>pemname</em>, can have any
identifier (such as <tt class="docutils literal"><span class="pre">run</span></tt>, <tt class="docutils literal"><span class="pre">life-cycle</span></tt>, etc). It can have
parameters.</p>
<p>For example, to activate the process object <tt class="docutils literal"><span class="pre">cust</span></tt> using the PEM
with identifier, <tt class="docutils literal"><span class="pre">lifetime</span></tt> at time 10.0 we would use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">cust</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">cust</span><span class="o">.</span><span class="n">lifetime</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;cust001&#39;</span><span class="p">),</span><span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><em>p.</em><tt class="docutils literal"><span class="pre">start([</span></tt><em>p.</em><tt class="docutils literal"><span class="pre">ACTIONS()]</span></tt> <tt class="docutils literal"><span class="pre">[,{at=now()|delay=0}][,prior=False])</span></tt></p>
<p>if <tt class="docutils literal"><span class="pre">p</span></tt> is a Process object and the generator function is given
the <em>standard identifier</em>, <tt class="docutils literal"><span class="pre">ACTIONS</span></tt>.  <tt class="docutils literal"><span class="pre">ACTIONS</span></tt>, is
recognized as a Process Execution Method. It may <em>not</em> have
parameters. The call <em>p.</em><tt class="docutils literal"><span class="pre">ACTIONS()</span></tt> is optional.</p>
<p>For example, to activate the process object <tt class="docutils literal"><span class="pre">cust</span></tt> with the standard
PEM identifier <tt class="docutils literal"><span class="pre">ACTIONS</span></tt> at time 10.0, the following are
equivalent (and the second version is more convenient):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">cust</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">cust</span><span class="o">.</span><span class="n">ACTIONS</span><span class="p">(),</span> <span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
<span class="n">cust</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">An <em>anonymous</em> instance of Process class <em>PR</em> can be created and
activated in one command using <tt class="docutils literal"><span class="pre">start</span></tt> with the standard PEM
identifier, <tt class="docutils literal"><span class="pre">ACTIONS</span></tt>.</p>
<p><em>PR.</em><tt class="docutils literal"><span class="pre">([args]).start(</span></tt> <tt class="docutils literal"><span class="pre">[,{at=now()|delay=0}][,prior=False])</span></tt></p>
<p>Here, <em>PR</em> is the identifier for the Process class and not for a
Process object as was <em>p</em>, in the statements above. The generator
method <tt class="docutils literal"><span class="pre">ACTIONS</span></tt> may <em>not</em> have parameters.</p>
<p>For example, if <tt class="docutils literal"><span class="pre">Customer</span></tt> is a SimPy Process class we can create
and activate an anonymous instance at time 10.0:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Customer</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;cust001&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">at</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>You can use the <tt class="docutils literal"><span class="pre">passivate</span></tt>, <tt class="docutils literal"><span class="pre">reactivate</span></tt>, or <tt class="docutils literal"><span class="pre">cancel</span></tt> commands to
control Process objects.</p>
</div>
<div class="section" id="passivate">
<span id="index-16"></span><h4>passivate<a class="headerlink" href="#passivate" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">passivate,self</span></tt></p>
<p>suspends the process object itself. It becomes &#8220;passive&#8221;. To get
it going again another process must <tt class="docutils literal"><span class="pre">reactivate</span></tt> it.</p>
</li>
</ul>
<span class="target" id="index-17"></span></div>
<div class="section" id="reactivate">
<span id="index-18"></span><h4>reactivate<a class="headerlink" href="#reactivate" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">reactivate(</span></tt><em>p</em><tt class="docutils literal"><span class="pre">[,{at=now()|delay=0}][,prior=False])</span></tt></p>
<p>reactivates a passive process object, <em>p</em>.  It becomes
&#8220;active&#8221;. The optional parameters work as for <tt class="docutils literal"><span class="pre">activate</span></tt>. A
process object cannot reactivate itself.  To temporarily suspend
itself it must use <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold,self,t</span></tt> instead.</p>
</li>
</ul>
<span class="target" id="index-19"></span></div>
<div class="section" id="cancel">
<span id="index-20"></span><h4>cancel<a class="headerlink" href="#cancel" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.cancel(</span></tt><em>p</em><tt class="docutils literal"><span class="pre">)</span></tt></p>
<p>deletes all scheduled future events for process object <em>p</em>.  A
process cannot <tt class="docutils literal"><span class="pre">cancel</span></tt> itself.  If that is required, use
<tt class="docutils literal"><span class="pre">yield</span> <span class="pre">passivate,self</span></tt> instead. Only &#8220;active&#8221; process objects
can be canceled.</p>
</li>
</ul>
<p>A process object is &#8220;terminated&#8221; after all statements in its process
execution method have been completed. If the object is still
referenced by a variable, it becomes just a data container. This can be useful for
extracting information.  Otherwise, it is automatically destroyed.</p>
<p>Even activated process objects will not start operating until the
<tt class="docutils literal"><span class="pre">simulate(until=</span></tt><em>endtime</em><tt class="docutils literal"><span class="pre">)</span></tt> statement is executed. This starts the
simulation going and it will continue until time <em>endtime</em> (unless it runs
out of events to execute or the command <tt class="docutils literal"><span class="pre">stopSimulation(</span> <span class="pre">)</span></tt> is
executed).</p>
<hr class="docutils" />
<p id="example-3"><span id="index-21"></span><strong>Example 3</strong> This simulates a firework with a time fuse.  We have put
in a few extra <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> commands for added suspense.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">simulate</span>


<span class="k">class</span> <span class="nc">Firework</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> firework launched&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>
        <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mf">10.0</span>  <span class="c"># wait 10.0 time units</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mf">1.0</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> tick&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>
        <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mf">10.0</span>  <span class="c"># wait another 10.0 time units</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> Boom!!&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>


<span class="n">initialize</span><span class="p">()</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">Firework</span><span class="p">()</span>  <span class="c"># create a Firework object, and</span>
                <span class="c"># activate it (with some default parameters)</span>
<span class="n">activate</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">execute</span><span class="p">(),</span> <span class="n">at</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>Here is the output. No formatting was attempted so it looks a bit
ragged:</p>
<div class="highlight-python"><pre>0 firework launched
11.0 tick
12.0 tick
13.0 tick
14.0 tick
15.0 tick
16.0 tick
17.0 tick
18.0 tick
19.0 tick
20.0 tick
30.0 Boom!!
</pre>
</div>
<hr class="docutils" />
</div>
<div class="section" id="a-source-fragment">
<span id="index-22"></span><h4>A source fragment<a class="headerlink" href="#a-source-fragment" title="Permalink to this headline">¶</a></h4>
<p>One useful program pattern is the <em>source</em>. This is a process object
with a Process Execution Method (PEM) that sequentially generates and activates
other process objects &#8211; it is a source of other process
objects. Random arrivals can be modelled using random intervals
between activations.</p>
<hr class="docutils" />
<p id="example-4"><span id="index-23"></span><strong>Example 4: A source</strong>.  Here a source creates and activates a series of
customers who arrive at regular intervals of 10.0 units of time. This
continues until the simulation time exceeds the specified <tt class="docutils literal"><span class="pre">finishTime</span></tt>
of 33.0.  (Of course, to model customers with random inter-arrival
times the <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> statement would use a random variate, such as
<tt class="docutils literal"><span class="pre">expovariate(</span> <span class="pre">)</span></tt>, instead of the constant 10.0 inter-arrival time used
here.)  The following example assumes that the <tt class="docutils literal"><span class="pre">Customer</span></tt> class has
previously been defined with a PEM called <tt class="docutils literal"><span class="pre">run</span></tt> that does not require
any arguments:</p>
<div class="highlight-python"><pre>class Source(Process):

    def execute(self, finish):
       while now() &lt; finish:
          c = Customer()         # create a new customer object, and
              # activate it (using default parameters)
          activate(c, c.run())
          print('%s %s) % (now(), 'customer')
          yield hold, self, 10.0

initialize()
g = Source()                     # create the Source object, g,
                                 # and activate it
activate(g, g.execute(finish=33.0), at=0.0)
simulate(until=100)</pre>
</div>
<hr class="docutils" />
</div>
</div>
<div class="section" id="asynchronous-interruptions">
<span id="index-24"></span><h3>Asynchronous interruptions<a class="headerlink" href="#asynchronous-interruptions" title="Permalink to this headline">¶</a></h3>
<p>An active process object can be interrupted by another but cannot
interrupt itself.</p>
<div class="section" id="interrupt">
<span id="index-25"></span><h4>interrupt<a class="headerlink" href="#interrupt" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.interrupt(victim)</span></tt></p>
<p>The <em>interrupter</em> process object uses its <tt class="docutils literal"><span class="pre">interrupt</span></tt> method to
interrupt the <em>victim</em> process object. The interrupt is just a
<em>signal</em>. After this statement, the <em>interrupter</em> process object
continues its PEM.</p>
<p>For the interrupt to have an immediate effect, the <em>victim</em> process
object must be <em>active</em> &#8211; that is it must have an event scheduled
for it (that is, it is &#8220;executing&#8221; a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> ). If the
<em>victim</em> is not active (that is, it is either <em>passive</em> or
<em>terminated</em>) the interrupt has no effect. For example, process
objects queuing for resource facilities cannot be interrupted
because they are <em>passive</em> during their queuing phase.</p>
</li>
</ul>
<p>If interrupted, the <em>victim</em> returns from its <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> statement
prematurely. It must then check to see if it has been interrupted by
calling:</p>
</div>
<div class="section" id="interrupted">
<span id="index-26"></span><h4>interrupted<a class="headerlink" href="#interrupted" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.interrupted(</span> <span class="pre">)</span></tt></p>
<p>which returns <tt class="docutils literal"><span class="pre">True</span></tt> if it has been interrupted. The <em>victim</em> can
then either continue in the current activity or switch to an
alternative, making sure it tidies up the current state, such as
releasing any resources it owns.</p>
</li>
</ul>
<span class="target" id="index-27"></span></div>
<div class="section" id="interruptcause">
<span id="index-28"></span><h4>interruptCause<a class="headerlink" href="#interruptcause" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.interruptCause</span></tt></p>
<p>when the <em>victim</em> has been interrupted, <tt class="docutils literal"><span class="pre">self.interruptCause</span></tt> is a
reference to the <em>interrupter</em> object.</p>
</li>
</ul>
</div>
<div class="section" id="interruptleft">
<span id="index-29"></span><h4>interruptLeft<a class="headerlink" href="#interruptleft" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.interruptLeft</span></tt></p>
<p>gives the time remaining in the interrupted <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt>. The
interruption is reset (that is, &#8220;turned off&#8221;) at the <em>victim&#8217;s</em>
next call to a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt>.</p>
</li>
</ul>
<p>..index:: interruptReset</p>
</div>
<div class="section" id="interruptreset">
<h4>interruptReset<a class="headerlink" href="#interruptreset" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">self.interruptReset(</span> <span class="pre">)</span></tt></p>
<p>will reset the interruption.</p>
</li>
</ul>
<p>It may be helpful to think of an interruption signal as instructing
the <em>victim</em> to determine whether it should interrupt itself. If the
<em>victim</em> determines that it should interrupt itself, it then becomes
responsible for making any necessary readjustments &#8211; not only to
itself but also to any other simulation components that are
affected. (The <em>victim</em> must take responsibility for these
adjustments, because it is the only simulation component that &#8220;knows&#8221;
such details as whether or not it is interrupting itself, when, and
why.)</p>
<hr class="docutils" />
<span class="target" id="index-30"></span><p id="example-5"><span id="index-31"></span><strong>Example 5</strong>. A simulation with interrupts.  A bus is subject to
breakdowns that are modelled as interrupts caused by a <tt class="docutils literal"><span class="pre">Breakdown</span></tt>
process.  Notice that the <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold,self,tripleft</span></tt> statement may
be interrupted, so if the <tt class="docutils literal"><span class="pre">self.interrupted()</span></tt> test returns <tt class="docutils literal"><span class="pre">True</span></tt>
a reaction to it is required. Here, in addition to delaying the bus
for repairs, the reaction includes scheduling the next breakdown. In
this example the <tt class="docutils literal"><span class="pre">Bus</span></tt> Process class does not require an
<tt class="docutils literal"><span class="pre">__init__()</span></tt> method:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Process</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span>
        <span class="n">reactivate</span><span class="p">,</span> <span class="n">simulate</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Bus</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">operate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">repairduration</span><span class="p">,</span> <span class="n">triplength</span><span class="p">):</span>  <span class="c"># PEM</span>
        <span class="n">tripleft</span> <span class="o">=</span> <span class="n">triplength</span>                   <span class="c"># time needed to finish trip</span>
        <span class="k">while</span> <span class="n">tripleft</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">tripleft</span>         <span class="c"># try to finish the trip</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">interrupted</span><span class="p">():</span>             <span class="c"># if another breakdown occurs</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> at </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">interruptCause</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">()))</span>
                <span class="n">tripleft</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">interruptLeft</span>   <span class="c"># time to finish the trip</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">interruptReset</span><span class="p">()</span>           <span class="c"># end interrupt state</span>
                <span class="n">reactivate</span><span class="p">(</span><span class="n">br</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="n">repairduration</span><span class="p">)</span>  <span class="c"># restart breakdown br</span>
                <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">repairduration</span>      <span class="c"># delay for repairs</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&#39;Bus repaired at </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>  <span class="c"># no more breakdowns, bus finished trip</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;Bus has arrived at </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>


<span class="k">class</span> <span class="nc">Breakdown</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">myBus</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;Breakdown &#39;</span> <span class="o">+</span> <span class="n">myBus</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bus</span> <span class="o">=</span> <span class="n">myBus</span>

    <span class="k">def</span> <span class="nf">breakBus</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interval</span><span class="p">):</span>       <span class="c"># process execution method</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">interval</span>  <span class="c"># breakdown interarrivals</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="o">.</span><span class="n">terminated</span><span class="p">():</span>
                <span class="k">break</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">interrupt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="p">)</span>    <span class="c"># breakdown to myBus</span>


<span class="n">initialize</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">Bus</span><span class="p">(</span><span class="s">&#39;Bus&#39;</span><span class="p">)</span>      <span class="c"># create a bus object</span>
<span class="n">activate</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">operate</span><span class="p">(</span><span class="n">repairduration</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">triplength</span><span class="o">=</span><span class="mi">1000</span><span class="p">))</span>
<span class="n">br</span> <span class="o">=</span> <span class="n">Breakdown</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>   <span class="c"># create breakdown br to bus b</span>
<span class="n">activate</span><span class="p">(</span><span class="n">br</span><span class="p">,</span> <span class="n">br</span><span class="o">.</span><span class="n">breakBus</span><span class="p">(</span><span class="mi">300</span><span class="p">))</span>
<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">4000</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;SimPy: No more events at time </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">now</span><span class="p">())</span>
</pre></div>
</td></tr></table></div>
<p>The output from this example:</p>
<div class="highlight-python"><pre>Breakdown Bus at 300
Bus repaired at 320
Breakdown Bus at 620
Bus repaired at 640
Breakdown Bus at 940
Bus repaired at 960
Bus has arrived at 1060
SimPy: No more events at time 1260
</pre>
</div>
<p>The bus finishes at 1060 but the simulation finished at 1260. Why? The
<tt class="docutils literal"><span class="pre">breakdown</span></tt>s PEM consists of a loop, one breakdown following
another at <tt class="docutils literal"><span class="pre">300</span></tt> intervals. The last breakdown finishes at 960 and
then a breakdown event is scheduled for 1260. But the bus finished at
1060 and is not affected by the breakdown.  These details can easily
be checked by importing from <tt class="docutils literal"><span class="pre">SimPy.SimulationTrace</span></tt> and re-running
the program.</p>
<hr class="docutils" />
<p>Where interrupts can occur, the victim of interrupts must test for
interrupt occurrence after every appropriate <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold</span></tt> and react
appropriately to it. A victim holding a resource facility when it gets
interrupted continues to hold it.</p>
</div>
</div>
<div class="section" id="advanced-synchronization-scheduling-capabilities">
<h3>Advanced synchronization/scheduling capabilities<a class="headerlink" href="#advanced-synchronization-scheduling-capabilities" title="Permalink to this headline">¶</a></h3>
<p>The preceding scheduling constructs all depend on specified time
values. That is, they delay processes for a specific time, or use
given time parameters when reactivating them. For a wide range of
applications this is all that is needed.</p>
<p>However, some applications either require or can profit from an
ability to activate processes that must wait for other processes to
complete. For example, models of real-time systems or operating
systems often use this kind of approach. <a class="reference internal" href="#event">Event</a> Signalling is
particularly helpful in such situations. Furthermore, some
applications need to activate processes when certain conditions occur,
even though when (or if) they will occur may be unknown. SimPy has a
general <a class="reference internal" href="#wait-until">wait until</a> to support clean implementation of this
approach.</p>
<p>This section describes how SimPy provides <a class="reference internal" href="#event">event</a> Signalling and <a class="reference internal" href="#wait-until">wait
until</a> capabilities.</p>
<span class="target" id="event"><span id="index-32"></span></span><div class="section" id="creating-and-signalling-simevents">
<span id="index-33"></span><h4>Creating and Signalling SimEvents<a class="headerlink" href="#creating-and-signalling-simevents" title="Permalink to this headline">¶</a></h4>
<p>As mentioned in the Introduction, for ease of expression when no
confusion can arise we often refer to both process objects and their
classes as &#8220;processes&#8221;, and mention their object or class status only
for added clarity or emphasis. Analogously, we will refer to objects
of SimPy&#8217;s <tt class="docutils literal"><span class="pre">SimEvent</span></tt> class as &#8220;SimEvents&#8221; <a class="footnote-reference" href="#id5" id="id6">[3]</a> (or, if no confusion
can arise, simply as &#8220;events&#8221;). However, we sometimes mention their
object or class character for clarity or emphasis.</p>
<table class="docutils footnote" frame="void" id="id7" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[4]</td><td>The name SimEvent was chosen because &#8220;event&#8221; is already
used in Python&#8217;s standard library. See Python Library Reference
section <em>7.5 threading &#8211; Higher-level threading interface</em>, specifically
subsection 7.5.5.</td></tr>
</tbody>
</table>
<p>SimEvent objects must be created before they can be fired by a
<tt class="docutils literal"><span class="pre">signal</span></tt>. You create the SimEvent object, <tt class="docutils literal"><span class="pre">sE</span></tt>, from SimPy&#8217;s
<tt class="docutils literal"><span class="pre">SimEvent</span></tt> class by a statement like the following:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">sE</span> <span class="pre">=</span> <span class="pre">SimEvent(name='I</span> <span class="pre">just</span> <span class="pre">had</span> <span class="pre">a</span> <span class="pre">great</span> <span class="pre">new</span> <span class="pre">idea!')</span></tt></div></blockquote>
<p>A SimEvent&#8217;s <tt class="docutils literal"><span class="pre">name</span></tt> attribute defaults to <tt class="docutils literal"><span class="pre">a_SimEvent</span></tt> unless you
provide your own, as shown here. Its <tt class="docutils literal"><span class="pre">occurred</span></tt> attribute,
<tt class="docutils literal"><span class="pre">sE.occurred</span></tt>, is a Boolean that defaults to <tt class="docutils literal"><span class="pre">False</span></tt>. It indicates
whether the event <tt class="docutils literal"><span class="pre">sE</span></tt> has occurred.</p>
<p>You program a SimEvent to &#8220;occur&#8221; or &#8220;fire&#8221; by &#8220;signalling&#8221; it like this:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">sE.signal(</span></tt><em>&lt;payload parameter&gt;</em><tt class="docutils literal"><span class="pre">)</span></tt></div></blockquote>
<p>This &#8220;signal&#8221; is &#8220;received&#8221; by all processes that are either &#8220;waiting&#8221;
or &#8220;queueing&#8221; for this event to occur. What happens when they receive
this signal is explained in the next section. The <em>&lt;payload
parameter&gt;</em> is optional &#8211; it defaults to <tt class="docutils literal"><span class="pre">None</span></tt>. It can be of any
Python type. Any process can retrieve it from the event&#8217;s
<tt class="docutils literal"><span class="pre">signalparam</span></tt> attribute, for example by:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">message</span> <span class="pre">=</span> <span class="pre">sE.signalparam</span></tt></div></blockquote>
<div class="section" id="waiting-or-queueing-for-simevents">
<span id="index-34"></span><h5>Waiting or Queueing for SimEvents<a class="headerlink" href="#waiting-or-queueing-for-simevents" title="Permalink to this headline">¶</a></h5>
<p>You can program a process either to &#8220;wait&#8221; or to &#8220;queue&#8221; for the
occurrence of SimEvents. The difference is that <em>all</em> processes
&#8220;waiting&#8221; for some event are reactivated as soon as it occurs. For
example, all firemen go into action when the alarm sounds. In
contrast, only the <em>first</em> process in the &#8220;queue&#8221; for some event is
reactivated when it occurs. That is, the &#8220;queue&#8221; is FIFO <a class="footnote-reference" href="#fifo" id="id8">[5]</a>. An example
might be royal succession &#8211; when the present ruler dies: &#8220;The king is
dead. Long live the (new) king!&#8221; (And all others in the line of
succession move up one step.)</p>
<table class="docutils footnote" frame="void" id="fifo" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[5]</td><td><em>(<a class="fn-backref" href="#id8">1</a>, <a class="fn-backref" href="#id9">2</a>, <a class="fn-backref" href="#id10">3</a>)</em> &#8220;First-in-First-Out&#8221; or FCFS, &#8220;First-Come-First-Served&#8221;</td></tr>
</tbody>
</table>
<p>You program a process to <tt class="docutils literal"><span class="pre">wait</span></tt> for SimEvents by including in its PEM:</p>
</div>
</div>
<div class="section" id="yield-waitevent">
<span id="index-35"></span><h4>yield waitevent<a class="headerlink" href="#yield-waitevent" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">waitevent,self,</span></tt><em>&lt;events part&gt;</em></p>
<p>where <em>&lt;events part&gt;</em> can be either:</p>
<ul class="simple">
<li>one SimEvent object, e.g. <tt class="docutils literal"><span class="pre">myEvent</span></tt>, or</li>
<li>a tuple of SimEvent objects, e.g. <tt class="docutils literal"><span class="pre">(myEvent,myOtherEvent,TimeOut)</span></tt>, or</li>
<li>a list of SimEvent objects, e.g. <tt class="docutils literal"><span class="pre">[myEvent,myOtherEvent,TimeOut]</span></tt></li>
</ul>
<p>If none of the events in the <em>&lt;events part&gt;</em> have occurred, the process
is passivated and joined to the list of processes waiting for some
event in <em>&lt;events part&gt;</em> to occur (or to recur).</p>
<p>On the other hand, when <em>any</em> of the events in the <em>&lt;events part&gt;</em>
occur, then <em>all</em> of the processes &#8220;waiting&#8221; for those particular
events are reactivated at the current time. Then the <tt class="docutils literal"><span class="pre">occurred</span></tt> flag
of those particular events is reset to <tt class="docutils literal"><span class="pre">False</span></tt>. Resetting their
<tt class="docutils literal"><span class="pre">occurred</span></tt> flag prevents the waiting processes from being constantly
reactivated. (For instance, we do not want firemen to keep responding
to any such &#8220;false alarms.&#8221;)  For example, suppose the <em>&lt;events part&gt;</em>
lists events <tt class="docutils literal"><span class="pre">a,</span> <span class="pre">b</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt>  in that order. If events <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt>
occur, then all of the processes waiting for event <tt class="docutils literal"><span class="pre">a</span></tt> are
reactivated. So are all processes waiting for event <tt class="docutils literal"><span class="pre">c</span></tt> but not
<tt class="docutils literal"><span class="pre">a</span></tt>. Then the <tt class="docutils literal"><span class="pre">occurred</span></tt> flags of events <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt> are toggled to
<tt class="docutils literal"><span class="pre">False</span></tt>. No direct changes are made to event <tt class="docutils literal"><span class="pre">b</span></tt> or to any processes
waiting for it to occur.</p>
</li>
</ul>
<p>You program a process to &#8220;queue&#8221; for events by including in its PEM:</p>
</div>
<div class="section" id="yield-queueevent">
<span id="index-36"></span><h4>yield queueevent<a class="headerlink" href="#yield-queueevent" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">queueevent,self,</span></tt><em>&lt;events part&gt;</em></p>
<p>where the  <em>&lt;events part&gt;</em> is as described above.</p>
<p>If none of the events in the <em>&lt;events part&gt;</em> has occurred, the process
is passivated and appended to the FIFO queue of processes queuing for
some event in <em>&lt;events part&gt;</em> to occur (or recur).</p>
<p>But when any of the events in <em>&lt;events part&gt;</em> occur, the process at
the head of the &#8220;queue&#8221; is taken off the queue and reactivated at the
current time. Then the <tt class="docutils literal"><span class="pre">occurred</span></tt> flag of those events that occurred
is reset to <tt class="docutils literal"><span class="pre">False</span></tt> as in the &#8220;waiting&#8221; case.</p>
</li>
</ul>
<span class="target" id="index-37"></span><span class="target" id="index-38"></span><div class="section" id="finding-which-processes-are-waiting-queueing-for-an-event-and-which-events-fired">
<span id="index-39"></span><h5>Finding Which Processes Are Waiting/Queueing for an Event, and Which Events Fired<a class="headerlink" href="#finding-which-processes-are-waiting-queueing-for-an-event-and-which-events-fired" title="Permalink to this headline">¶</a></h5>
<p>SimPy automatically keeps current lists of what processes are
&#8220;waiting&#8221; or &#8220;queueing&#8221; for SimEvents. They are kept in the <tt class="docutils literal"><span class="pre">waits</span></tt>
and <tt class="docutils literal"><span class="pre">queues</span></tt> attributes of the SimEvent object and can be read by
commands like the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">TheProcessesWaitingFor_myEvent</span> <span class="o">=</span> <span class="n">myEvent</span><span class="o">.</span><span class="n">waits</span>
<span class="n">TheProcessesQueuedFor_myEvent</span> <span class="o">=</span> <span class="n">myEvent</span><span class="o">.</span><span class="n">queues</span>
</pre></div>
</div>
<p>However, you should not attempt to change these attributes yourself.</p>
<p>Whenever <tt class="docutils literal"><span class="pre">myEvent</span></tt> occurs, i.e., whenever a <tt class="docutils literal"><span class="pre">myEvent.signal(...)</span></tt>
statement is executed, SimPy does the following:</p>
<ul class="simple">
<li>If there are any processes waiting or queued for that event, it
reactivates them as described in the preceding section.</li>
<li>If there are no processes waiting or queued (i.e., <tt class="docutils literal"><span class="pre">myEvent.waits</span></tt>
and <tt class="docutils literal"><span class="pre">myEvent.queues</span></tt> are both empty), it toggles <tt class="docutils literal"><span class="pre">myEvent.occurred</span></tt>
to <tt class="docutils literal"><span class="pre">True</span></tt>.</li>
</ul>
<p>SimPy also automatically keeps track of which events were fired when a
process object was reactivated. For example, you can get a list of the
events that were fired when the object <tt class="docutils literal"><span class="pre">Godzilla</span></tt> was reactivated with
a statement like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">GodzillaRevivedBy</span> <span class="o">=</span> <span class="n">Godzilla</span><span class="o">.</span><span class="n">eventsFired</span>
</pre></div>
</div>
<hr class="docutils" />
<span class="target" id="index-40"></span><p id="example-6"><span id="index-41"></span><strong>Example 6</strong>. This complete SimPy script illustrates these
constructs. (It also illustrates that a Process class may have more
than one PEM. Here the <tt class="docutils literal"><span class="pre">Wait_Or_Queue</span></tt> class has two PEMs &#8211;
<tt class="docutils literal"><span class="pre">waitup</span></tt> and <tt class="docutils literal"><span class="pre">queueup</span></tt>.):</p>
<div class="highlight-python"><pre>from SimPy.Simulation import *

class Wait_Or_Queue(Process):
    def waitup(self,myEvent):      # PEM illustrating "waitevent"
                                   # wait for "myEvent" to occur
        yield waitevent, self, myEvent
        print 'At %s, some SimEvent(s) occurred that \
        activated object %s.' %(now(), self.name)
        print '   The activating event(s) were %s' \
            %([x.name for x in self.eventsFired])

    def queueup(self, myEvent):    # PEM illustrating "queueevent"
                                   # queue up for "myEvent" to occur
        yield queueevent, self, myEvent
        print 'At %s, some SimEvent(s) occurred that \
        activated object %s.' %(now(), self.name)
        print '   The activating event(s) were %s' \
            %([x.name for x in self.eventsFired])

class Signaller(Process):
    # here we just schedule some events to fire
    def sendSignals(self):
        yield hold, self, 2
        event1.signal()        # fire "event1" at time 2
        yield hold, self, 8
        event2.signal()        # fire "event2" at time 10
        yield hold, self, 5
        event1.signal()        # fire all four events at time 15
        event2.signal()
        event3.signal()
        event4.signal()
        yield hold, self, 5
        event4.signal()        # event4 recurs at time 20

initialize()

# Now create each SimEvent and give it a name
event1 = SimEvent('Event-1')
event2 = SimEvent('Event-2')
event3 = SimEvent('Event-3')
event4 = SimEvent('Event-4')
Event_list = [event3,event4]   # define an event list

s = Signaller()
# Activate Signaller "s" *after* events created
activate (s,s.sendSignals())

w0 = Wait_Or_Queue('W-0')
# create object named "W-0", and set it to
# "waitup" for SimEvent "event1" to occur
activate (w0, w0.waitup(event1))
w1 = Wait_Or_Queue('W-1')
activate (w1, w1.waitup(event2))
w2 = Wait_Or_Queue('W-2')
activate(w2, w2.waitup(Event_list))
q1 = Wait_Or_Queue('Q-1')
# create object named "Q-1", and put it to be first
# in the queue for Event_list to occur
activate(q1, q1.queueup(Event_list))
q2 = Wait_Or_Queue('Q-2')
# create object named "Q-2", and append it to
# the queue for Event_list to occur
activate(q2, q2.queueup(Event_list))

simulate(until=50)
</pre>
</div>
<p>This program outputs:</p>
<div class="highlight-python"><pre>At 2, some SimEvent(s) occurred that         activated object W-0.
   The activating event(s) were ['Event-1']
At 10, some SimEvent(s) occurred that         activated object W-1.
   The activating event(s) were ['Event-2']
At 15, some SimEvent(s) occurred that         activated object W-2.
   The activating event(s) were ['Event-3']
At 15, some SimEvent(s) occurred that         activated object Q-1.
   The activating event(s) were ['Event-3', 'Event-4']
At 20, some SimEvent(s) occurred that         activated object Q-2.
   The activating event(s) were ['Event-4']
</pre>
</div>
<p>Each output line, <tt class="docutils literal"><span class="pre">The</span> <span class="pre">activating</span> <span class="pre">event(s)</span> <span class="pre">were</span> <span class="pre">...</span></tt>, lists the
contents of the named object&#8217;s <tt class="docutils literal"><span class="pre">eventsFired</span></tt> attribute. One of those
events &#8220;caused&#8221; the object to reactivate at the indicated time. Note
that at time 15 objects <tt class="docutils literal"><span class="pre">W-0</span></tt> and <tt class="docutils literal"><span class="pre">W-1</span></tt> were not affected by the
recurrence of <tt class="docutils literal"><span class="pre">event1</span></tt> and <tt class="docutils literal"><span class="pre">event2</span></tt> because they already were
active. Also at time 15, even though objects <tt class="docutils literal"><span class="pre">W-2</span></tt>, <tt class="docutils literal"><span class="pre">Q-1</span></tt> and <tt class="docutils literal"><span class="pre">Q-2</span></tt>
were all waiting for <tt class="docutils literal"><span class="pre">event3</span></tt>, only <tt class="docutils literal"><span class="pre">W-2</span></tt> and <tt class="docutils literal"><span class="pre">Q-1</span></tt> were
reactivated. Process object <tt class="docutils literal"><span class="pre">Q-2</span></tt> was not reactivated at that time
because it was not first in the queue.  Finally, <tt class="docutils literal"><span class="pre">Q-2</span></tt> was reactivated
at time 20, when <tt class="docutils literal"><span class="pre">event4</span></tt> fired again.</p>
<hr class="docutils" />
</div>
</div>
<div class="section" id="waituntil-synchronization-waiting-for-any-condition">
<span id="wait-until"></span><span id="index-42"></span><h4>&#8220;waituntil&#8221; synchronization &#8211; waiting for any condition<a class="headerlink" href="#waituntil-synchronization-waiting-for-any-condition" title="Permalink to this headline">¶</a></h4>
<p>SimPy provides the <tt class="docutils literal"><span class="pre">waituntil</span></tt> feature that makes a process&#8217;s
progress depend on the state of the simulation. This is useful if, for
example, you need to reactivate a process when (if ever) the
simulation enters the state <tt class="docutils literal"><span class="pre">goodWeather</span> <span class="pre">OR</span> <span class="pre">(nrCustomers&gt;50</span> <span class="pre">AND</span>
<span class="pre">price&lt;22.50)</span></tt>.  Doing that requires <em>interrogative</em> scheduling,
while all other SimPy synchronization constructs are <em>imperative</em> &#8211;
i.e., the condition must be tested after every change in state until
it becomes <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>This requires that after every change in system state SimPy must run a
special (hidden) process that tests and responds appropriately to the
condition&#8217;s truth-value. This clearly takes more run time than SimPy&#8217;s
imperative scheduling constructs. So SimPy activates its interrogative
testing process only so long as at least one process is executing a
<tt class="docutils literal"><span class="pre">waituntil</span></tt> statement. When this is not the case, the run time overhead
is minimal (about 1 percent extra run time).</p>
<span class="target" id="index-43"></span></div>
<div class="section" id="yield-waituntil">
<span id="index-44"></span><h4>yield waituntil<a class="headerlink" href="#yield-waituntil" title="Permalink to this headline">¶</a></h4>
<p>You program a process to wait for a condition to be satisfied by
including in its PEM a statement of the form:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">waituntil,</span> <span class="pre">self,</span></tt><em>&lt;cond&gt;</em></div></blockquote>
<p>where <em>&lt;cond&gt;</em> is a reference to a function, without parameters, that
returns a Boolean value indicating whether the simulation state or
condition to be waited for has occurred.</p>
<hr class="docutils" />
<p id="example-7"><span id="romulans"></span><span id="index-45"></span><strong>Example 7</strong>. This program using the <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">waituntil</span> <span class="pre">...</span></tt>
statement. Here the function <tt class="docutils literal"><span class="pre">killed()</span></tt>, in the <tt class="docutils literal"><span class="pre">life()</span></tt> PEM of
the <tt class="docutils literal"><span class="pre">Player</span></tt> process, defines the condition to be waited for</p>
<div class="highlight-python"><pre>from SimPy.Simulation import *
import random

class Player(Process):

  def __init__(self,lives=1,name='ImaTarget'):
    Process.__init__(self,name)
    self.lives=lives
      # provide Player objects with a "damage" property
    self.damage=0

  def life(self):
    self.message='Drat! Some %s survived Federation attack!' %(target.name)

    def killed():     # function testing for "damage &gt; 5"
      return self.damage&gt;5

    while True:
      yield waituntil,self,killed
      self.lives-=1; self.damage=0
      if self.lives==0:
        self.message= '%s wiped out by Federation at \
          time %s!' %(target.name,now())
        stopSimulation()

class Federation(Process):

  def fight(self):                # simulate Federation operations
    print 'Three %s attempting to escape!' %(target.name)
    while True:
      if random.randint(0,10)&lt;2:  # check for hit on player
        target.damage+=1          # hit! increment damage to player
        if target.damage &lt;= 5:    # target survives
          print 'Ha! %s hit! Damage = %i'%(target.name, target.damage)
        else:
          if (target.lives-1)==0:
            print 'No more %s left!' %(target.name)
          else:
            print 'Now only %i %s left!' %(target.lives-1,target.name)

      yield hold,self,1

initialize()
gameOver=100
  # create a Player object named "Romulans"
target=Player(lives=3,name='Romulans')
activate(target,target.life())
  # create a Federation object
shooter=Federation()
activate(shooter,shooter.fight())
simulate(until=gameOver)
print target.message</pre>
</div>
<p>One possible output from this program is shown below. Whether the
Romulans are wiped out or some escape depends on what simulation
states the randomization feature produces:</p>
<div class="highlight-python"><pre>Three Romulans attempting to escape!
Ha! Romulans hit! Damage = 1
Ha! Romulans hit! Damage = 2
Ha! Romulans hit! Damage = 3
Ha! Romulans hit! Damage = 4
Ha! Romulans hit! Damage = 5
  Now only 2 Romulans left!
Ha! Romulans hit! Damage = 1
Ha! Romulans hit! Damage = 2
Ha! Romulans hit! Damage = 3
Ha! Romulans hit! Damage = 4
Ha! Romulans hit! Damage = 5
  Now only 1 Romulans left!
Ha! Romulans hit! Damage = 1
Ha! Romulans hit! Damage = 2
Ha! Romulans hit! Damage = 3
Ha! Romulans hit! Damage = 4
Ha! Romulans hit! Damage = 5
  No more Romulans left!
Romulans wiped out by Federation at time 73!</pre>
</div>
<hr class="docutils" />
<p>The <tt class="docutils literal"><span class="pre">waituntil</span></tt> construct is so general that in principle it could
replace all the other synchronization approaches (but at a run time
cost).</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
</div>
<div class="section" id="resources">
<span id="index-46"></span><h2><a class="toc-backref" href="#id19">Resources</a><a class="headerlink" href="#resources" title="Permalink to this headline">¶</a></h2>
<p>The three resource facilities provided by SimPy are <a class="reference internal" href="#resources">Resources</a>,
<a class="reference internal" href="#levels">Levels</a> and <a class="reference internal" href="#stores">Stores</a>. Each models a congestion point where process
objects may have to queue up to obtain resources. This section
describes the Resource type of resource facility.</p>
<p>An example of queueing for a Resource might be a manufacturing plant
in which a <tt class="docutils literal"><span class="pre">Task</span></tt> (modelled as a <em>process object</em>) needs work done
by a <tt class="docutils literal"><span class="pre">Machine</span></tt> (modelled as a Resource object). If all of the
<tt class="docutils literal"><span class="pre">Machines</span></tt> are currently being used, the <tt class="docutils literal"><span class="pre">Task</span></tt> must wait until
one becomes free. A SimPy Resource can have a number of identical
<tt class="docutils literal"><span class="pre">units</span></tt>, such as a number of identical <tt class="docutils literal"><span class="pre">machine</span></tt> units. A
process obtains a unit of the Resource by <tt class="docutils literal"><span class="pre">requesting</span></tt> it and, when
it is finished, <tt class="docutils literal"><span class="pre">releasing</span></tt> it. A Resource maintains a list of
process objects that have requested but not yet received one of the
Resource&#8217;s units (called the <tt class="docutils literal"><span class="pre">waitQ</span></tt>), and another list of processes
that are currently using a unit (the <tt class="docutils literal"><span class="pre">activeQ</span></tt>).  SimPy creates and
updates these queues itself &#8211; the user can access them, but should
not change them.</p>
<div class="section" id="defining-a-resource-object">
<span id="index-47"></span><h3>Defining a Resource object<a class="headerlink" href="#defining-a-resource-object" title="Permalink to this headline">¶</a></h3>
<p>A Resource object, <tt class="docutils literal"><span class="pre">r</span></tt>,  is established by the following statement:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">r</span> <span class="o">=</span> <span class="n">Resource</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;a_resource&#39;</span><span class="p">,</span> <span class="n">unitName</span><span class="o">=</span><span class="s">&#39;units&#39;</span><span class="p">,</span>
           <span class="n">qType</span><span class="o">=</span><span class="n">FIFO</span><span class="p">,</span> <span class="n">preemptable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
           <span class="n">monitored</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">monitorType</span><span class="o">=</span><span class="n">Monitor</span><span class="p">)</span>
</pre></div>
</div>
<p>where</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">capacity</span></tt> is a positive real or integer value that specifies the total
number of identical units in Resource object <tt class="docutils literal"><span class="pre">r</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">name</span></tt> is a descriptive name for this Resource object (e.g.,
<tt class="docutils literal"><span class="pre">'gasStation'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">unitName</span></tt> is a descriptive name for a unit of the resource
(e.g., <tt class="docutils literal"><span class="pre">'pump'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">qType</span></tt> is either <tt class="docutils literal"><span class="pre">FIFO</span></tt> <a class="footnote-reference" href="#fifo" id="id9">[5]</a> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt>. It
specifies the queue discipline of the resource&#8217;s <tt class="docutils literal"><span class="pre">waitQ</span></tt>;
typically, this is <tt class="docutils literal"><span class="pre">FIFO</span></tt> and that is the default value. If
<tt class="docutils literal"><span class="pre">PriorityQ</span></tt> is specified, then higher-priority requests waiting
for a unit of Resource <tt class="docutils literal"><span class="pre">r</span></tt> are inserted into the <tt class="docutils literal"><span class="pre">waitQ</span></tt> ahead
of lower priority requests.  See <a class="reference internal" href="#priority-requests-for-a-resource-unit">Priority requests for a Resource
unit</a> for details.</li>
<li><tt class="docutils literal"><span class="pre">preemptable</span></tt> is a Boolean (<tt class="docutils literal"><span class="pre">False</span></tt> or <tt class="docutils literal"><span class="pre">True</span></tt>); typically,
this is <tt class="docutils literal"><span class="pre">False</span></tt> and that is the default value. If it is
<tt class="docutils literal"><span class="pre">True</span></tt>, then a process requesting a unit of this resource may
preempt a lower-priority process in the <tt class="docutils literal"><span class="pre">activeQ</span></tt>, i.e., one that
is already using a unit of the resource.  See <a class="reference internal" href="#preemptive-requests-for-a-resource-unit">Preemptive requests
for a Resource unit</a> for details.</li>
<li><tt class="docutils literal"><span class="pre">monitored</span></tt> is a boolean (<tt class="docutils literal"><span class="pre">False</span></tt> or <tt class="docutils literal"><span class="pre">True</span></tt>). If set to
<tt class="docutils literal"><span class="pre">True</span></tt>, then information is gathered on the sizes of <tt class="docutils literal"><span class="pre">r</span></tt>&#8216;s
<tt class="docutils literal"><span class="pre">waitQ</span></tt> and <tt class="docutils literal"><span class="pre">activeQ</span></tt>, otherwise not.</li>
<li><tt class="docutils literal"><span class="pre">monitorType</span></tt> is either <tt class="docutils literal"><span class="pre">Monitor</span></tt> or <tt class="docutils literal"><span class="pre">Tally</span></tt> and indicates the
type of <a class="reference internal" href="#recording-simulation-results">Recorder</a> to be used (see <a class="reference internal" href="#recording-resource-queue-lengths">Recording Resource queue lengths</a> for an example and additional
discussion).</li>
</ul>
</div></blockquote>
<p>Each Resource object, <tt class="docutils literal"><span class="pre">r</span></tt>,  has the following additional attributes:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">r.n</span></tt>,  the number of units that are currently free.</li>
<li><tt class="docutils literal"><span class="pre">r.waitQ</span></tt>, a queue (list) of processes that have requested but
not yet received a unit of <tt class="docutils literal"><span class="pre">r</span></tt>, so <tt class="docutils literal"><span class="pre">len(r.waitQ)</span></tt> is the
number of process objects currently waiting.</li>
<li><tt class="docutils literal"><span class="pre">r.activeQ</span></tt>, a queue (list) of process objects currently using
one of the Resource&#8217;s units, so <tt class="docutils literal"><span class="pre">len(r.activeQ)</span></tt> is the number of
units that are currently in use.</li>
<li><tt class="docutils literal"><span class="pre">r.waitMon</span></tt>, the record (made by a <tt class="docutils literal"><span class="pre">Monitor</span></tt> or a <tt class="docutils literal"><span class="pre">Tally</span></tt>
whenever <tt class="docutils literal"><span class="pre">monitored==True</span></tt>) of the activity in <tt class="docutils literal"><span class="pre">r.waitQ</span></tt>. So,
for example, <tt class="docutils literal"><span class="pre">r.waitMon.timeaverage()</span></tt> is the average number of
processes in <tt class="docutils literal"><span class="pre">r.waitQ</span></tt>.  See <a class="reference internal" href="#recording-resource-queue-lengths">Recording Resource queue lengths</a>
for an example.</li>
<li><tt class="docutils literal"><span class="pre">r.actMon</span></tt>, the record (made by a <tt class="docutils literal"><span class="pre">Monitor</span></tt> or a <tt class="docutils literal"><span class="pre">Tally</span></tt> whenever
<tt class="docutils literal"><span class="pre">monitored==True</span></tt>) of the activity in <tt class="docutils literal"><span class="pre">r.activeQ</span></tt>.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="requesting-and-releasing-a-unit-of-a-resource">
<h3>Requesting and releasing a unit of a Resource<a class="headerlink" href="#requesting-and-releasing-a-unit-of-a-resource" title="Permalink to this headline">¶</a></h3>
<p>A process can request and later release a unit of the Resource object,
<tt class="docutils literal"><span class="pre">r</span></tt>, by using the following yield commands in a Process Execution
Method:</p>
<div class="section" id="yield-request">
<span id="index-48"></span><h4>yield request<a class="headerlink" href="#yield-request" title="Permalink to this headline">¶</a></h4>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">request,self,</span></tt><em>r [,P=0]</em></p>
<p>requests a unit of Resource <em>r</em> with (optional) real or integer
priority value <em>P</em>. If no priority is specified, it defaults to 0.
Larger values of <em>P</em> represent higher priorities. See the following
sections on <a class="reference internal" href="#queue-order">Queue Order</a> for more information on how priority
values are used.  Although this form of request can be used for
either <tt class="docutils literal"><span class="pre">FIFO</span></tt> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt> priority types, these values are
<em>ignored</em> when <tt class="docutils literal"><span class="pre">qType==FIFO</span></tt>.</p>
</li>
</ul>
</div>
<div class="section" id="yield-release">
<span id="index-49"></span><h4>yield release<a class="headerlink" href="#yield-release" title="Permalink to this headline">¶</a></h4>
<blockquote>
<div><p><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">release,self,r</span></tt></p>
<p>releases the  unit of <em>r</em>.</p>
</div></blockquote>
</div>
</div>
<div class="section" id="queue-order">
<span id="index-50"></span><h3>Queue Order<a class="headerlink" href="#queue-order" title="Permalink to this headline">¶</a></h3>
<p>If a requesting process must wait it is placed into the resource&#8217;s
<tt class="docutils literal"><span class="pre">waitQ</span></tt> in an order determined by settings of the resource&#8217;s
<tt class="docutils literal"><span class="pre">qType</span></tt> and <tt class="docutils literal"><span class="pre">preemptable</span></tt> attributes and of the priority value it
uses in the <tt class="docutils literal"><span class="pre">request</span></tt> call.</p>
<div class="section" id="non-priority-queueing">
<span id="index-51"></span><h4>Non-priority queueing<a class="headerlink" href="#non-priority-queueing" title="Permalink to this headline">¶</a></h4>
<p>If the <tt class="docutils literal"><span class="pre">qType</span></tt> is not specified it takes the presumed value of
<tt class="docutils literal"><span class="pre">FIFO</span></tt> <a class="footnote-reference" href="#fifo" id="id10">[5]</a>. In that case processes wait in the usual
first-come-first-served order.</p>
<p>If a Resource unit is free when the request is made, the requesting
process takes it and moves on to the next statement in its PEM. If no
Resource unit is available when the request is made, then the
requesting process is appended to the Resource&#8217;s <tt class="docutils literal"><span class="pre">waitQ</span></tt> and
suspended.  The next time a unit becomes available the first process
in the <tt class="docutils literal"><span class="pre">r.waitQ</span></tt> takes it and continues its execution. All priority
assignments are ignored. Moreover, in the FIFO case no preemption is
possible, for preemption requires that priority assignments be
recognized. (However, see the <a class="reference internal" href="#note-on-preemptive-requests-with-waitq-in-fifo-order">Note on preemptive requests with waitQ
in FIFO order</a> for one way of simulating such situations.)</p>
<hr class="docutils" />
<p><strong>Example</strong> In this complete script, the <tt class="docutils literal"><span class="pre">server</span></tt> Resource object is
given two resource units (<tt class="docutils literal"><span class="pre">capacity=2</span></tt>). By not specifying its
<tt class="docutils literal"><span class="pre">Qtype</span></tt> it takes the default value, <tt class="docutils literal"><span class="pre">FIFO</span></tt>.  Here six clients
arrive in the order specified by the program. They all request a
resource unit from the <tt class="docutils literal"><span class="pre">server</span></tt> Resource object at the same
time. Even though they all specify a priority value in their requests,
it is ignored and they get their Resource units in the same order as
their requests:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Process</span><span class="p">,</span> <span class="n">Resource</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span>
        <span class="n">now</span><span class="p">,</span> <span class="n">release</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">simulate</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Client</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="n">inClients</span> <span class="o">=</span> <span class="p">[]</span>     <span class="c"># list the clients in order by their requests</span>
    <span class="n">outClients</span> <span class="o">=</span> <span class="p">[]</span>    <span class="c"># list the clients in order by completion of service</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">getserved</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">servtime</span><span class="p">,</span> <span class="n">priority</span><span class="p">,</span> <span class="n">myServer</span><span class="p">):</span>
        <span class="n">Client</span><span class="o">.</span><span class="n">inClients</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> requests 1 unit at t = </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">()))</span>
        <span class="c"># request use of a resource unit</span>
        <span class="k">yield</span> <span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">myServer</span><span class="p">,</span> <span class="n">priority</span>
        <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">servtime</span>
        <span class="c"># release the resource</span>
        <span class="k">yield</span> <span class="n">release</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">myServer</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> done at t = </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">()))</span>
        <span class="n">Client</span><span class="o">.</span><span class="n">outClients</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>


<span class="n">initialize</span><span class="p">()</span>

<span class="c"># the next line creates the ``server`` Resource object</span>
<span class="n">server</span> <span class="o">=</span> <span class="n">Resource</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>  <span class="c"># server defaults to qType==FIFO</span>

<span class="c"># the next lines create some Client process objects</span>
<span class="n">c1</span><span class="p">,</span> <span class="n">c2</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c1&#39;</span><span class="p">),</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c2&#39;</span><span class="p">)</span>
<span class="n">c3</span><span class="p">,</span> <span class="n">c4</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c3&#39;</span><span class="p">),</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c4&#39;</span><span class="p">)</span>
<span class="n">c5</span><span class="p">,</span> <span class="n">c6</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c5&#39;</span><span class="p">),</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c6&#39;</span><span class="p">)</span>

<span class="c"># in the next lines each client requests</span>
<span class="c"># one of the ``server``&#39;s Resource units</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c1</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c2</span><span class="p">,</span> <span class="n">c2</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c3</span><span class="p">,</span> <span class="n">c3</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c4</span><span class="p">,</span> <span class="n">c4</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c5</span><span class="p">,</span> <span class="n">c5</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c6</span><span class="p">,</span> <span class="n">c6</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>

<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s">&#39;Request order: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">Client</span><span class="o">.</span><span class="n">inClients</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;Service order: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">Client</span><span class="o">.</span><span class="n">outClients</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>This program results in the following output:</p>
<div class="highlight-python"><pre>c1 requests 1 unit at t = 0
c2 requests 1 unit at t = 0
c3 requests 1 unit at t = 0
c4 requests 1 unit at t = 0
c5 requests 1 unit at t = 0
c6 requests 1 unit at t = 0
c1 done at time = 100
c2 done at time = 100
c3 done at time = 200
c4 done at time = 200
c5 done at time = 300
c6 done at time = 300

Request order:  ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']
Service order:  ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']</pre>
</div>
<p>As illustrated, the clients are served in FIFO order. Clients <tt class="docutils literal"><span class="pre">c1</span></tt>
and <tt class="docutils literal"><span class="pre">c2</span></tt> each take one Resource unit right away, but the others must
wait. When <tt class="docutils literal"><span class="pre">c1</span></tt> and <tt class="docutils literal"><span class="pre">c2</span></tt> finish with their resources, clients
<tt class="docutils literal"><span class="pre">c3</span></tt> and <tt class="docutils literal"><span class="pre">c4</span></tt> can each take a unit, and so forth.</p>
</div>
<div class="section" id="priority-requests-for-a-resource-unit">
<span id="index-52"></span><h4>Priority requests for a Resource unit<a class="headerlink" href="#priority-requests-for-a-resource-unit" title="Permalink to this headline">¶</a></h4>
<p>If the Resource <tt class="docutils literal"><span class="pre">r</span></tt> is defined with <tt class="docutils literal"><span class="pre">qType==PriorityQ</span></tt>, priority
values in requests are recognized.  If a Resource unit is available
when the request is made, the requesting process takes it. If no
Resource unit is available when the request is made, the requesting
process is inserted into the Resource&#8217;s <tt class="docutils literal"><span class="pre">waitQ</span></tt> in order of priority
(from high to low) and suspended. For an example where priorities are
used, we simply change the preceding example&#8217;s specification of the
<tt class="docutils literal"><span class="pre">server</span></tt> Resource object to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">server</span><span class="o">=</span><span class="n">Resource</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">qType</span><span class="o">=</span><span class="n">PriorityQ</span><span class="p">)</span>
</pre></div>
</div>
<p>where, by not specifying it, we allow <tt class="docutils literal"><span class="pre">preemptable</span></tt> to take its
default value, <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<hr class="docutils" />
<p><strong>Example</strong> After this change the program&#8217;s output becomes:</p>
<div class="highlight-python"><pre>c1 requests 1 unit at t = 0
c2 requests 1 unit at t = 0
c3 requests 1 unit at t = 0
c4 requests 1 unit at t = 0
c5 requests 1 unit at t = 0
c6 requests 1 unit at t = 0
c1 done at time = 100
c2 done at time = 100
c6 done at time = 200
c5 done at time = 200
c4 done at time = 300
c3 done at time = 300

Request order:  ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']
Service order:  ['c1', 'c2', 'c6', 'c5', 'c4', 'c3']</pre>
</div>
<p>Although <tt class="docutils literal"><span class="pre">c1</span></tt> and <tt class="docutils literal"><span class="pre">c2</span></tt> have the lowest priority values, each requested
and got a <tt class="docutils literal"><span class="pre">server</span></tt> unit immediately. That was because at the time they
made those requests a <tt class="docutils literal"><span class="pre">server</span></tt> unit was available and the
<tt class="docutils literal"><span class="pre">server.waitQ</span></tt> was empty &#8211; it did not start to fill until <tt class="docutils literal"><span class="pre">c3</span></tt> made
its request and found all of the <tt class="docutils literal"><span class="pre">server</span></tt> units busy. When <tt class="docutils literal"><span class="pre">c1</span></tt> and
<tt class="docutils literal"><span class="pre">c2</span></tt> completed service, <tt class="docutils literal"><span class="pre">c6</span></tt> and <tt class="docutils literal"><span class="pre">c5</span></tt> (with the highest priority
values of all processes in the <tt class="docutils literal"><span class="pre">waitQ</span></tt>) each got a Resource unit,
etc.</p>
<hr class="docutils" />
<p>When some processes in the <tt class="docutils literal"><span class="pre">waitQ</span></tt> have the same priority level as a
process making a priority request, SimPy inserts the requesting
process immediately <em>behind</em> them. Thus for a given priority value,
processes are placed in FIFO order. For example, suppose that when a
&#8220;priority 3&#8221; process makes its priority request the current <tt class="docutils literal"><span class="pre">waitQ</span></tt>
consists of processes with priorities <tt class="docutils literal"><span class="pre">[5,4,3a,3b,3c,2a,2b,1]</span></tt>,
where the letters indicate the order in which the equal-priority
processes were placed in the queue.  Then SimPy inserts this
requesting process into the current <tt class="docutils literal"><span class="pre">waitQ</span></tt> immediately behind its
last &#8220;priority 3&#8221; process. Thus, the new <tt class="docutils literal"><span class="pre">waitQ</span></tt> will be
<tt class="docutils literal"><span class="pre">[5,4,3a,3b,3c,3d,2a,2b,1]</span></tt>, where the inserted process is <tt class="docutils literal"><span class="pre">3d</span></tt>.</p>
<p>One consequence of this is that, if all priority requests are assigned
the same priority value, then the <tt class="docutils literal"><span class="pre">waitQ</span></tt> will in fact be maintained
in <tt class="docutils literal"><span class="pre">FIFO</span></tt> order. In that case, using a <tt class="docutils literal"><span class="pre">FIFO</span></tt> instead of a
<tt class="docutils literal"><span class="pre">PriorityQ</span></tt> discipline provides some saving in execution time which
may be important in simulations where the <tt class="docutils literal"><span class="pre">waitQ</span></tt> may be long.</p>
</div>
<div class="section" id="preemptive-requests-for-a-resource-unit">
<span id="index-53"></span><h4>Preemptive requests for a Resource unit<a class="headerlink" href="#preemptive-requests-for-a-resource-unit" title="Permalink to this headline">¶</a></h4>
<p>In some models, higher priority processes can actually <em>preempt</em> lower
priority processes, i.e., they can take over and use a Resource unit
currently being used by a lower priority process whenever no free
Resource units are available. A Resource object that allows its units
to be preempted is created by setting its properties to
<tt class="docutils literal"><span class="pre">qType==PriorityQ</span></tt> and <tt class="docutils literal"><span class="pre">preemptable==True</span></tt>.</p>
<p>Whenever a <tt class="docutils literal"><span class="pre">preemptable</span></tt> Resource unit is free when a request is
made, then the requesting process takes it and continues its
execution. On the other hand, when a higher priority request finds all
the units in a <tt class="docutils literal"><span class="pre">preemptable</span></tt> Resource in use, then SimPy adopts the
following procedure regarding the Resource&#8217;s <tt class="docutils literal"><span class="pre">activeQ</span></tt> and
<tt class="docutils literal"><span class="pre">waitQ</span></tt>:</p>
<ul class="simple">
<li>The process with the lowest priority is removed from the <tt class="docutils literal"><span class="pre">activeQ</span></tt>,
suspended, and put at the front of the <tt class="docutils literal"><span class="pre">waitQ</span></tt> &#8211; so (barring
additional preemptions) it will be the next one to get a resource
unit.</li>
<li>The preempting process gets the vacated resource unit and is
inserted into the <tt class="docutils literal"><span class="pre">activeQ</span></tt> in order of its priority value.</li>
<li>The time for which the preempted process had the resource unit is
taken into account when the process gets into the <tt class="docutils literal"><span class="pre">activeQ</span></tt> again.
Thus, its <em>total hold time</em> is always the same, regardless of how
many times it has been preempted.</li>
</ul>
<p id="index-54">Warning: SimPy only supports preemption of processes which are
implemented in the following pattern:</p>
<div class="highlight-python"><pre>yield request (one or more request statements)
&lt;some code&gt;
yield hold (one or more hold statements)
&lt;some code&gt;
yield release (one or more release statements)</pre>
</div>
<p>Modelling the preemption of a process in any other pattern may lead to
errors or exceptions.</p>
<p>We emphasize that a process making a <tt class="docutils literal"><span class="pre">preemptive</span></tt> request to a
fully-occupied Resource gets a resource unit if &#8211; but only if &#8211; some
process in the current <tt class="docutils literal"><span class="pre">activeQ</span></tt> has a lower priority. Otherwise, it
will be inserted into the <tt class="docutils literal"><span class="pre">waitQ</span></tt> at a location determined by its
priority value and the current contents of the <tt class="docutils literal"><span class="pre">waitQ</span></tt>, using a
procedure analogous to that described for priority requests near the
end of the preceding section on <a class="reference internal" href="#priority-requests-for-a-resource-unit">Priority requests for a Resource
unit</a>. This may have the effect of advancing the preempting process
ahead of any lower-priority processes that had earlier been preempted
and put at the head of the <tt class="docutils literal"><span class="pre">waitQ</span></tt>. In fact, if several preemptions
occur before a unit of resource is freed up, then the head of the
<tt class="docutils literal"><span class="pre">waitQ</span></tt> will consist of the processes that have been preempted &#8211; in
order from the last process preempted to the first of them.</p>
<hr class="docutils" />
<p><strong>Example</strong> In this example two clients of different priority compete
for the same resource unit:</p>
<div class="highlight-python"><pre>from SimPy.Simulation import *

class Client(Process):
   def __init__(self,name):
      Process.__init__(self,name)

   def getserved(self,servtime,priority,myServer):
       print self.name, 'requests 1 unit at t=',now()
       yield request, self, myServer, priority
       yield hold, self, servtime
       yield release, self,myServer
       print self.name,'done at t= ',now()

initialize()
# create the *server* Resource object
server=Resource(capacity=1,qType=PriorityQ,preemptable=1)
# create some Client process objects
c1=Client(name='c1')
c2=Client(name='c2')
activate(c1,c1.getserved(servtime=100,priority=1,myServer=server),at=0)
activate(c2,c2.getserved(servtime=100,priority=9,myServer=server),at=50)
simulate(until=500)</pre>
</div>
<p>The output from this program is:</p>
<div class="highlight-python"><pre>c1 requests 1 unit at t= 0
c2 requests 1 unit at t= 50
c2 done at t= 150
c1 done at t= 200</pre>
</div>
<p>Here, <tt class="docutils literal"><span class="pre">c1</span></tt> is preempted by <tt class="docutils literal"><span class="pre">c2</span></tt> at <tt class="docutils literal"><span class="pre">t=50</span></tt>. At that time, <tt class="docutils literal"><span class="pre">c1</span></tt>
had held the resource for 50 of its total of 100 time units. When
<tt class="docutils literal"><span class="pre">c2</span></tt> finished and released the resource unit at 150, <tt class="docutils literal"><span class="pre">c1</span></tt> got the
resource back and finished the last 50 time units of its service at
<tt class="docutils literal"><span class="pre">t=200</span></tt>.</p>
<hr class="docutils" />
<p>If preemption occurs when the last few processes in the current
<tt class="docutils literal"><span class="pre">activeQ</span></tt> have the same priority value, then the last process in the
current <tt class="docutils literal"><span class="pre">activeQ</span></tt> is the one that will be preempted and inserted
into the <tt class="docutils literal"><span class="pre">waitQ</span></tt> ahead of all others.  To describe this, it will be
convenient to indicate by an added letter the order in which
equal-priority processes have been inserted into a queue. Now, suppose
that a &#8220;priority 4&#8221; process makes a preemptive request when the
current <tt class="docutils literal"><span class="pre">activeQ</span></tt> priorities are <tt class="docutils literal"><span class="pre">[5,3a,3b]</span></tt> and the current
<tt class="docutils literal"><span class="pre">waitQ</span></tt> priorities are <tt class="docutils literal"><span class="pre">[2,1,0a,0b]</span></tt>. Then process <tt class="docutils literal"><span class="pre">3b</span></tt> will be
preempted.  After the preemption the <tt class="docutils literal"><span class="pre">activeQ</span></tt> will be <tt class="docutils literal"><span class="pre">[5,4,3a]</span></tt>
and the <tt class="docutils literal"><span class="pre">waitQ</span></tt> will be <tt class="docutils literal"><span class="pre">[3b,2,1,0a,0b]</span></tt>.</p>
</div>
<div class="section" id="note-on-preemptive-requests-with-waitq-in-fifo-order">
<h4>Note on preemptive requests with waitQ in FIFO order<a class="headerlink" href="#note-on-preemptive-requests-with-waitq-in-fifo-order" title="Permalink to this headline">¶</a></h4>
<p>You may consider doing the following to model a system whose queue of
items waiting for a resource is to be maintained in FIFO order, but in
which preemption is to be possible. It uses SimPy&#8217;s <tt class="docutils literal"><span class="pre">preemptable</span></tt>
Resource objects, and uses priorities in a way that allows for
preempts while maintaining a FIFO <tt class="docutils literal"><span class="pre">waitQ</span></tt> order.</p>
<ul class="simple">
<li>Set <tt class="docutils literal"><span class="pre">qType=PriorityQ</span></tt> and <tt class="docutils literal"><span class="pre">preemptable=True</span></tt> (so that SimPy
will process preemptive requests correctly).</li>
<li>Model &#8220;system requests that are to be considered as non-preemptive&#8221;
in SimPy as process objects each of which has exactly the same
(low) priority value &#8211; for example, either assign all of them a
priority value of 0 (zero) or let it default to that value.  (This
has the effect of maintaining all of these process objects in the
<tt class="docutils literal"><span class="pre">waitQ</span></tt> in FIFO order, as explained at the end of the section on
<a class="reference internal" href="#priority-requests-for-a-resource-unit">Priority requests for a Resource unit</a>, above.)</li>
<li>Model &#8220;system requests that are to be considered as preemptive&#8221; in
SimPy as process objects each of which is assigned a uniform
priority value, but give them a higher value than the one used to
model the &#8220;non-preemptive system requests&#8221; &#8211; for example, assign
all of them a priority value of 1 (one). Then they will have a higher
priority value than any of the non-preemptive requests.</li>
</ul>
<hr class="docutils" />
<p><strong>Example</strong> Here is an example of how this works for a Resource with
two Resource units &#8211; we give the <tt class="docutils literal"><span class="pre">activeQ</span></tt> before the <tt class="docutils literal"><span class="pre">waitQ</span></tt>
throughout this example:</p>
<ol class="arabic simple">
<li>Suppose that the current <tt class="docutils literal"><span class="pre">activeQ</span></tt> and <tt class="docutils literal"><span class="pre">waitQ</span></tt> are <tt class="docutils literal"><span class="pre">[0a,0b]</span></tt>
and <tt class="docutils literal"><span class="pre">[0c],</span></tt> respectively.</li>
<li>A &#8220;priority 1&#8221; process makes a preemptive request. Then the queues
become: <tt class="docutils literal"><span class="pre">[1a,0a]</span></tt> and`` [0b,0c]``.</li>
<li>Another &#8220;priority 1&#8221; process makes a preemptive request. Then the
queues become: <tt class="docutils literal"><span class="pre">[1a,1b]</span></tt> and <tt class="docutils literal"><span class="pre">[0a,0b,0c]</span></tt>.</li>
<li>A third &#8220;priority 1&#8221; process makes a preemptive request. Then the
queues become: <tt class="docutils literal"><span class="pre">[1a,1b]</span></tt> and <tt class="docutils literal"><span class="pre">[1c,0a,0b,0c]</span></tt>.</li>
<li>Process <tt class="docutils literal"><span class="pre">1a</span></tt> finishes using its resource unit. Then the queues
become: <tt class="docutils literal"><span class="pre">[1b,1c]</span></tt> and <tt class="docutils literal"><span class="pre">[0a,0b,0c]</span></tt>.</li>
</ol>
<hr class="docutils" />
</div>
</div>
<div class="section" id="reneging-leaving-a-queue-before-acquiring-a-resource">
<span id="index-55"></span><h3>Reneging &#8211; leaving a queue before acquiring a resource<a class="headerlink" href="#reneging-leaving-a-queue-before-acquiring-a-resource" title="Permalink to this headline">¶</a></h3>
<p>In most real world situations, people and other items do not wait
forever for a requested resource facility to become
available. Instead, they leave its queue when their patience is
exhausted or when some other condition occurs. This behaviour is
called <em>reneging</em>, and the reneging person or thing is said to
<em>renege</em>.</p>
<p>SimPy provides an extended (i.e., compound) <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">request</span></tt> statement
to handle reneging.</p>
<div class="section" id="reneging-yield-request">
<span id="index-56"></span><h4>Reneging yield request<a class="headerlink" href="#reneging-yield-request" title="Permalink to this headline">¶</a></h4>
<p>There are two types of reneging clause, one for reneging after a
certain time and one for reneging when an event has happened.
Their general form is</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(request,self,</span></tt><em>r [,P]</em><tt class="docutils literal"><span class="pre">),</span></tt><tt class="docutils literal"><span class="pre">(</span></tt><em>&lt;reneging clause&gt;</em><tt class="docutils literal"><span class="pre">)</span></tt></div></blockquote>
<p>to request a unit of Resource <em>r</em> (with optional priority <em>P</em>,
assuming the Resource has been defined as a <tt class="docutils literal"><span class="pre">priorityQ</span></tt>) but with
reneging.</p>
<p>A SimPy program that models Resource requests with reneging must use
the following pattern of statements:</p>
<div class="highlight-python"><pre>yield (request,self,r),(&lt;reneging clause&gt;)
if self.acquired(resource):
   ## process got resource and so did NOT renege
   . . . .
   yield release,self,resource
else:
   ## process reneged before acquiring resource
   . . . . .</pre>
</div>
<p>A call to the <tt class="docutils literal"><span class="pre">self.acquired(resource)</span></tt> method is mandatory after a
compound <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">request</span></tt> statement.  It not only indicates whether or
not the process has acquired the resource, it also removes the
reneging process from the resource&#8217;s <tt class="docutils literal"><span class="pre">waitQ</span></tt>.</p>
</div>
<div class="section" id="reneging-after-a-time-limit">
<span id="index-57"></span><h4>Reneging after a time limit<a class="headerlink" href="#reneging-after-a-time-limit" title="Permalink to this headline">¶</a></h4>
<p>To make a process give up (renege) after a certain time, use a
reneging clause of the following form:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(request,self,</span></tt><em>r [,P]</em><tt class="docutils literal"><span class="pre">),</span></tt><tt class="docutils literal"><span class="pre">(hold,self,</span></tt><em>waittime</em><tt class="docutils literal"><span class="pre">)</span></tt></div></blockquote>
<p>Here the process requests one unit of the resource <em>r</em> with optional
priority <em>P</em>. If a resource unit is available it takes it and continues
its PEM. Otherwise, as usual, it is passivated and inserted into <em>r</em>&#8216;s <tt class="docutils literal"><span class="pre">waitQ</span></tt>.</p>
<p>The process takes a unit if it becomes available before <em>waittime</em>
expires and continues executing its PEM. If, however, the process has
not acquired a unit before the <em>waittime</em> has expired it abandons the
request (reneges) and leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt>.</p>
<hr class="docutils" />
<p><strong>Example</strong>: part of a parking lot simulation:</p>
<div class="highlight-python"><pre>. . . .
parking_lot=Resource(capacity=10)
patience=5   # wait no longer than "patience" time units
             # for a parking space
park_time=60 # park for "park_time" time units if get a parking space
. . . .
yield (request,self,parking_lot),(hold,self,patience)
if self.acquired(parking_lot):
   # park the car
   yield hold,self,park_time
   yield release,self,parking_lot
else:
   # patience exhausted, so give up
   print 'I'm not waiting any longer. I am going home now.'</pre>
</div>
<hr class="docutils" />
</div>
<div class="section" id="reneging-when-an-event-has-happened">
<span id="index-58"></span><h4>Reneging when an event has happened<a class="headerlink" href="#reneging-when-an-event-has-happened" title="Permalink to this headline">¶</a></h4>
<p>To make a process renege at the occurrence of an event, use a reneging
clause having a pattern like the one used for a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">waitevent</span></tt>
statement, namely <tt class="docutils literal"><span class="pre">waitevent,self,</span></tt><em>events</em> (see <a class="reference internal" href="#yield-waitevent">yield waitevent</a>).
For example:</p>
<blockquote>
<div><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(request,self,</span></tt><em>r [,P]</em><tt class="docutils literal"><span class="pre">),</span></tt><tt class="docutils literal"><span class="pre">(waitevent,self,</span></tt><em>events</em><tt class="docutils literal"><span class="pre">)</span></tt></div></blockquote>
<p>Here the process requests one unit of the resource <em>r</em> with optional
priority <em>P</em>. If a resource unit is available it takes it and continues
its PEM. Otherwise, as usual, it is passivated and inserted into <em>r</em>&#8216;s <tt class="docutils literal"><span class="pre">waitQ</span></tt>.</p>
<p>The process takes a unit if it becomes available before any of the
<em>events</em> occur, and continues executing its PEM. If, however, any of
the SimEvents in <em>events</em> occur first, it abandons the
request (reneges) and leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt>. (Recall that <em>events</em> can be
either one event, a list, or a tuple of several SimEvents.)</p>
<hr class="docutils" />
<p><strong>Example</strong> Queuing for movie tickets (part):</p>
<div class="highlight-python"><pre>. . . .
seats=Resource(capacity=100)
sold_out=SimEvent() # signals "out of seats"
too_late=SimEvent() # signals "too late for this show"
. . . .
# Leave the ticket counter queue when movie sold out
# or it is too late for the show
yield (request,self,seats),(waitevent,self,[sold_out,too_late])
if self.acquired(seats):
   # watch the movie
   yield hold,self,120
   yield release,self,seats
else:
   # did not get a seat
   print 'Who needs to see this silly movie anyhow?'</pre>
</div>
</div>
<hr class="docutils" />
<div class="section" id="exiting-conventions-and-preemptive-queues">
<h4>Exiting conventions and preemptive queues<a class="headerlink" href="#exiting-conventions-and-preemptive-queues" title="Permalink to this headline">¶</a></h4>
<p>Many discrete event simulations (including SimPy) adopt the normal
&#8220;exiting convention&#8221;, according to which processes that have once
started using a Resource unit stay in some Resource queue until their
<tt class="docutils literal"><span class="pre">hold</span></tt> time has completed. This is of course automatically the case
for FIFO and non-preemptable <tt class="docutils literal"><span class="pre">PriorityQ</span></tt> disciplines. The point is
that the exiting convention is also applied in the <tt class="docutils literal"><span class="pre">preemptable</span></tt> queue
discipline case. Thus, processes remain in some Resource queue until
their <tt class="docutils literal"><span class="pre">hold</span></tt> time has completed, even if they are preempted by higher
priority processes.</p>
<p>Some real-world situations conform to this convention and some do
not. An example of one that does conform can be described as
follows. Suppose that at work you are assigned tasks of varying levels
of priority. You are to set aside lower priority tasks in order to
work on higher priority ones. But you are eventually to complete all
of your assigned tasks. So you are operating like a SimPy resource
that obeys a <tt class="docutils literal"><span class="pre">preemptable</span></tt> queue discipline and has one resource
unit. With this convention, half-finished low-priority tasks may be
postponed indefinitely if they are continually preempted by
higher-priority tasks.</p>
<p>An example that does not conform to the exiting convention can
be described as follows. Suppose again that you are assigned tasks of
varying levels of priority and are to set aside lower priority tasks
to work on higher priority ones. But you are instructed that any tasks
not completed within 24 hours after being assigned are to
be sent to another department for completion. Now, suppose
that you are assigned Task-A that has a priority level of 3 and will
take 10 hours to complete. After working on Task-A for an hour, you
are assigned Task-B, which has a priority level of 5 and will take 20
hours to complete. Then, at 11 hours, after working on Task-B for 10
hours, you are assigned Task-C, which has a priority level of 1 and
will take 4 hours to complete. (At this point Task-B needs 10 hours to
complete, Task-A needs 9 hours to complete, and Task-C needs 4 hours
to complete.) At 21 hours you complete Task-B and resume working on
Task-A, which at that point needs 9 hours to complete. At 24 hours
Task-A still needs another 6 hours to complete, but it has reached the
24-hour deadline and so is sent to another department for
completion. At the same time, Task-C has been in the waitQ for 13
hours, so you take it up and complete it at hour 28. This queue
discipline does not conform to the exiting convention, for under that
convention at 24 hours you would continue work on Task-A,
complete it at hour 30, and then start on Task-C.</p>
</div>
</div>
<div class="section" id="recording-resource-queue-lengths">
<span id="index-59"></span><h3>Recording Resource queue lengths<a class="headerlink" href="#recording-resource-queue-lengths" title="Permalink to this headline">¶</a></h3>
<p>Many discrete event models are used mainly to explore the
statistical properties of the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and <tt class="docutils literal"><span class="pre">activeQ</span></tt> associated with
some or all of their simulated resources. SimPy&#8217;s support for this
includes the <a class="reference internal" href="#defining-tallys-and-monitors">Monitor</a> and the <a class="reference internal" href="#defining-tallys-and-monitors">Tally</a>. For more information on
these and other recording
methods, see the section on <a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a>.</p>
<p>If a Resource, <tt class="docutils literal"><span class="pre">r</span></tt>, is defined with <tt class="docutils literal"><span class="pre">monitored=True</span></tt> SimPy
automatically records the length of its associated <tt class="docutils literal"><span class="pre">waitQ</span></tt> and
<tt class="docutils literal"><span class="pre">activeQ</span></tt>. These records are kept in the recorder objects called
<tt class="docutils literal"><span class="pre">r.waitMon</span></tt> and <tt class="docutils literal"><span class="pre">r.actMon</span></tt>, respectively. This solves a problem,
particularly for the <tt class="docutils literal"><span class="pre">waitQ</span></tt> which cannot easily be recorded
externally to the resource.</p>
<p>The property <tt class="docutils literal"><span class="pre">monitorType</span></tt> indicates which variety of recorder is to
be used, either <a class="reference internal" href="#defining-tallys-and-monitors">Monitor</a> or <a class="reference internal" href="#defining-tallys-and-monitors">Tally</a>. The default is <tt class="docutils literal"><span class="pre">Monitor</span></tt>. If
this is chosen, complete time series for both queue lengths are
maintained and can be used for advanced post-simulation statistical
analyses as well as for displaying summary statistics (such as
averages, standard deviations, and histograms). If <tt class="docutils literal"><span class="pre">Tally</span></tt> is chosen
summary statistics can be displayed, but complete time series
cannot. For more information on these and SimPy&#8217;s other recording
methods, see the section on <a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a>.</p>
<hr class="docutils" />
<p><strong>Example</strong> The following program uses a <tt class="docutils literal"><span class="pre">Monitor</span></tt> to record the
<tt class="docutils literal"><span class="pre">server</span></tt> resource&#8217;s queues.  After the simulation ends, it displays
some summary statistics for each queue, and then their complete time
series:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Monitor</span><span class="p">,</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Resource</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span>
        <span class="n">hold</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">release</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">simulate</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Client</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
        <span class="n">inClients</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">outClients</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
            <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">getserved</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">servtime</span><span class="p">,</span> <span class="n">myServer</span><span class="p">):</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> requests 1 unit at t = </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">()))</span>
            <span class="k">yield</span> <span class="n">request</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">myServer</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">servtime</span>
            <span class="k">yield</span> <span class="n">release</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">myServer</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> done at t = </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">now</span><span class="p">()))</span>


<span class="n">initialize</span><span class="p">()</span>

<span class="n">server</span> <span class="o">=</span> <span class="n">Resource</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">monitored</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">monitorType</span><span class="o">=</span><span class="n">Monitor</span><span class="p">)</span>

<span class="n">c1</span><span class="p">,</span> <span class="n">c2</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c1&#39;</span><span class="p">),</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c2&#39;</span><span class="p">)</span>
<span class="n">c3</span><span class="p">,</span> <span class="n">c4</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c3&#39;</span><span class="p">),</span> <span class="n">Client</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;c4&#39;</span><span class="p">)</span>

<span class="n">activate</span><span class="p">(</span><span class="n">c1</span><span class="p">,</span> <span class="n">c1</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c2</span><span class="p">,</span> <span class="n">c2</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c3</span><span class="p">,</span> <span class="n">c3</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>
<span class="n">activate</span><span class="p">(</span><span class="n">c4</span><span class="p">,</span> <span class="n">c4</span><span class="o">.</span><span class="n">getserved</span><span class="p">(</span><span class="n">servtime</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">myServer</span><span class="o">=</span><span class="n">server</span><span class="p">))</span>

<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(TimeAverage no. waiting: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">waitMon</span><span class="o">.</span><span class="n">timeAverage</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) Average no. waiting: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">waitMon</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) Var of no. waiting: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">waitMon</span><span class="o">.</span><span class="n">var</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) SD of no. waiting: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">server</span><span class="o">.</span><span class="n">waitMon</span><span class="o">.</span><span class="n">var</span><span class="p">()))</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(TimeAverage no. in service: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">actMon</span><span class="o">.</span><span class="n">timeAverage</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) Average no. in service: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">actMon</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) Var of no. in service: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">server</span><span class="o">.</span><span class="n">actMon</span><span class="o">.</span><span class="n">var</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;(Number) SD of no. in service: </span><span class="si">%.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">server</span><span class="o">.</span><span class="n">actMon</span><span class="o">.</span><span class="n">var</span><span class="p">()))</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;=&#39;</span> <span class="o">*</span> <span class="mi">40</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;Time history for the &quot;server&quot; waitQ:&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;[time, waitQ]&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">server</span><span class="o">.</span><span class="n">waitMon</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;=&#39;</span> <span class="o">*</span> <span class="mi">40</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;Time history for the &quot;server&quot; activeQ:&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;[time, activeQ]&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">server</span><span class="o">.</span><span class="n">actMon</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>The output from this program is:</p>
<div class="highlight-python"><pre>c1 requests 1 unit at t = 0
c2 requests 1 unit at t = 0
c3 requests 1 unit at t = 0
c4 requests 1 unit at t = 0
c1 done at t = 100
c2 done at t = 200
c3 done at t = 300
c4 done at t = 400

(Time) Average no. waiting: 1.5
(Number) Average no. waiting: 1.5
(Number) Var of no. waiting: 0.916666666667
(Number) SD of no. waiting: 0.957427107756
(Time) Average no. in service: 1.0
(Number) Average no. in service: 0.5
(Number) Var of no. in service: 0.25
(Number) SD of no. in service: 0.5
========================================
Time history for the 'server' waitQ:
[time, waitQ]
[0, 1]
[0, 2]
[0, 3]
[100, 2]
[200, 1]
[300, 0]
========================================
Time history for the 'server' activeQ:
[time, activeQ]
[0, 1]
[100, 0]
[100, 1]
[200, 0]
[200, 1]
[300, 0]
[300, 1]
[400, 0]</pre>
</div>
<p>This output illustrates the difference between the <em>(Time) Average</em>
and the <em>number statistics</em>. Here process <tt class="docutils literal"><span class="pre">c1</span></tt> was in the <tt class="docutils literal"><span class="pre">waitQ</span></tt>
for zero time units, process <tt class="docutils literal"><span class="pre">c2</span></tt> for 100 time units, and so
forth. The total wait time accumulated by all four processes during
the entire simulation run, which ended at time 400, amounts to 0 + 100
+ 200 + 300 = 600 time units. Dividing the 600 accumulated time units
by the simulation run time of 400 gives 1.5 for the <em>(Time) Average</em>
number of processes in the <tt class="docutils literal"><span class="pre">waitQ</span></tt>. It is the time-weighted average
length of the <tt class="docutils literal"><span class="pre">waitQ</span></tt>, but is almost always called simply the average
length of the <tt class="docutils literal"><span class="pre">waitQ</span></tt> or the average number of items waiting for a
resource.</p>
<p>It is also the expected number of processes you would find in the
<tt class="docutils literal"><span class="pre">waitQ</span></tt> if you took a snapshot of it at a random time during the
simulation. The <tt class="docutils literal"><span class="pre">activeQ</span></tt>&#8216;s time average computation is similar,
although in this example the resource is held by some process
throughout the simulation. Even though the number in the <tt class="docutils literal"><span class="pre">activeQ</span></tt>
momentarily drops to zero as one process releases the resource and
immediately rises to one as the next process acquires it, that occurs
instantaneously and so contributes nothing to the <em>(Time) Average</em>
computation.</p>
<hr class="docutils" />
<p><em>Number statistics</em> such as the Average, Variance, and Standard
Deviation are computed differently. At time zero the number of
processes in the <tt class="docutils literal"><span class="pre">waitQ</span></tt> starts at 1, then rises to 2, and then
to 3. At time 100 it drops back to two processes, and so forth. The
average and standard deviation of the six values [1, 2, 3, 2, 1, 0] is
1.5 and 0.9574..., respectively. Number statistics for the <tt class="docutils literal"><span class="pre">activeQ</span></tt>
are computed using the eight values [1, 0, 1, 0, 1, 0, 1, 0] and are
as shown in the output.</p>
<p>When the <tt class="docutils literal"><span class="pre">monitorType</span></tt> is changed to <tt class="docutils literal"><span class="pre">Tally</span></tt>, all the output up to
and including the lines:</p>
<div class="highlight-python"><pre>Time history for the 'server' waitQ:
[time, waitQ]</pre>
</div>
<p>is displayed. Then the output concludes with an error message
indicating a problem with the reference to <tt class="docutils literal"><span class="pre">server.waitMon</span></tt>. Of
course, this is because <tt class="docutils literal"><span class="pre">Tally</span></tt> does not generate complete time
series.</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
<div class="section" id="levels">
<span id="index-60"></span><h2><a class="toc-backref" href="#id20">Levels</a><a class="headerlink" href="#levels" title="Permalink to this headline">¶</a></h2>
<p>The three resource facilities provided by the SimPy system are
<a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a> and <a class="reference internal" href="#stores">Stores</a>. Each models a congestion point where
process objects may have to queue up to obtain resources. This section
describes the Level type of resource facility.</p>
<p>Levels model the production and consumption of a homogeneous
undifferentiated &#8220;material.&#8221; Thus, the currently-available amount of
material in a Level resource facility can be fully described by a
scalar (real or integer). Process objects may increase or decrease the
currently-available amount of material in a Level facility.</p>
<p>For example, a gasoline station stores gas (petrol) in large
tanks. Tankers increase, and refuelled cars decrease, the amount of gas
in the station&#8217;s storage tanks. Both getting amounts and putting
amounts may be subjected to <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">reneging</a> like requesting amounts from a
Resource.</p>
<div class="section" id="defining-a-level">
<span id="index-61"></span><h3>Defining a Level<a class="headerlink" href="#defining-a-level" title="Permalink to this headline">¶</a></h3>
<p>You define the Level resource facility <em>lev</em> by a statement like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lev</span> <span class="o">=</span> <span class="n">Level</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;a_level&#39;</span><span class="p">,</span> <span class="n">unitName</span><span class="o">=</span><span class="s">&#39;units&#39;</span><span class="p">,</span>
            <span class="n">capacity</span><span class="o">=</span><span class="s">&#39;unbounded&#39;</span><span class="p">,</span> <span class="n">initialBuffered</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
            <span class="n">putQType</span><span class="o">=</span><span class="n">FIFO</span><span class="p">,</span> <span class="n">getQType</span><span class="o">=</span><span class="n">FIFO</span><span class="p">,</span>
            <span class="n">monitored</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">monitorType</span><span class="o">=</span><span class="n">Monitor</span><span class="p">)</span>
</pre></div>
</div>
<p>where</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">name</span></tt> (string type) is a descriptive name for the Level object <em>lev</em> is
known (e.g., <tt class="docutils literal"><span class="pre">'inventory'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">unitName</span></tt> (string type) is a descriptive name for the units in which the
amount of material in <em>lev</em> is measured (e.g., <tt class="docutils literal"><span class="pre">'kilograms'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">capacity</span></tt> (positive real or integer) is the capacity of the Level
object <em>lev</em>.  The default value is set to <tt class="docutils literal"><span class="pre">'unbounded'</span></tt> which is
interpreted as <tt class="docutils literal"><span class="pre">sys.maxint</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">initialBuffered</span></tt> (positive real or integer) is the initial amount of
material in the Level object <em>lev</em>.</li>
<li><tt class="docutils literal"><span class="pre">putQType</span></tt> (<tt class="docutils literal"><span class="pre">FIFO</span></tt> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt>) is the (producer) queue
discipline.</li>
<li><tt class="docutils literal"><span class="pre">getQType</span></tt> (<tt class="docutils literal"><span class="pre">FIFO</span></tt> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt>) is the (consumer) queue
discipline.</li>
<li><tt class="docutils literal"><span class="pre">monitored</span></tt> (boolean) specifies whether the queues and the amount
of material in <em>lev</em> will be recorded.</li>
<li><tt class="docutils literal"><span class="pre">monitorType</span></tt> (<tt class="docutils literal"><span class="pre">Monitor</span></tt> or <tt class="docutils literal"><span class="pre">Tally</span></tt>) specifies which type of
<a class="reference internal" href="#recording-simulation-results">Recorder</a> to use. Defaults to <tt class="docutils literal"><span class="pre">Monitor</span></tt>.</li>
</ul>
</div></blockquote>
<p>Every Level resource object, such as <tt class="docutils literal"><span class="pre">lev</span></tt>, also has the following
additional attributes:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">lev.amount</span></tt> is the amount currently held in <em>lev</em>.</li>
<li><tt class="docutils literal"><span class="pre">lev.putQ</span></tt> is the queue of processes waiting to add amounts to <em>lev</em>,
so <tt class="docutils literal"><span class="pre">len(lev.putQ)</span></tt> is the number of processes waiting to add
amounts.</li>
<li><tt class="docutils literal"><span class="pre">lev.getQ</span></tt> is the queue of processes waiting to get amounts from
<em>lev</em>, so <tt class="docutils literal"><span class="pre">len(lev.getQ)</span></tt> is the number of processes waiting to
get amounts.</li>
<li><tt class="docutils literal"><span class="pre">lev.monitored</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt> if the queues are to be recorded. In
this case <tt class="docutils literal"><span class="pre">lev.putQMon</span></tt>, <tt class="docutils literal"><span class="pre">lev.getQMon</span></tt>, and <tt class="docutils literal"><span class="pre">lev.bufferMon</span></tt>
exist.</li>
<li><tt class="docutils literal"><span class="pre">lev.putQMon</span></tt> is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">lev.putQ</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">lev.getQMon</span></tt> is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">lev.getQ</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">lev.bufferMon</span></tt> is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">lev.amount</span></tt>.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="getting-amounts-from-a-level">
<span id="index-62"></span><h3>Getting amounts from a Level<a class="headerlink" href="#getting-amounts-from-a-level" title="Permalink to this headline">¶</a></h3>
<p>Processes can request amounts from a Level and the same or other
processes can offer amounts to it.</p>
<p>A process, the <em>requester</em>, can request an amount <em>ask</em> from the
Level resource object <em>lev</em> by a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get</span></tt> statement.:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get,self,</span></tt><em>lev,ask[,P]</em></li>
</ul>
<p>Here <em>ask</em> must be a positive real or integer (the amount) and <em>P</em> is
an optional priority value (real or integer). If <em>lev</em> does not hold
enough to satisfy the request (that is, <em>ask &gt; lev</em><tt class="docutils literal"><span class="pre">.amount</span></tt>) the
requesting process is passivated and queued (in <em>lev</em><tt class="docutils literal"><span class="pre">.getQ</span></tt>) in order
of its priority. Subject to the priority order, it will be reactivated
when there is enough to satisfy the request.</p>
<p><tt class="docutils literal"><span class="pre">self.got</span></tt> holds the amount actually received by the requester.</p>
<p>..index:: Level; put</p>
</div>
<div class="section" id="putting-amounts-into-a-level">
<h3>Putting amounts into  a Level<a class="headerlink" href="#putting-amounts-into-a-level" title="Permalink to this headline">¶</a></h3>
<p>A process, the <em>offerer</em>, which is usually but not necessarily
different from the <em>requester</em>, can offer an amount <em>give</em> to a Level,
<em>lev</em>, by a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put</span></tt> statement:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put,self,</span></tt><em>lev,give[,P]</em></li>
</ul>
<p>Here <em>give</em> must be a positive real or integer, and <em>P</em> is an optional
priority value (real or integer). If the amount offered would lead to
an overflow (that is, <em>lev.</em><tt class="docutils literal"><span class="pre">amount</span></tt> + <em>give &gt; lev.</em><tt class="docutils literal"><span class="pre">capacity</span></tt>)  the offering process is passivated and queued (in
<tt class="docutils literal"><span class="pre">lev.putQ</span></tt>). Subject to the priority order, it will be reactivated
when there is enough space to hold the amount offered.</p>
<p>The orderings of processes in a Level&#8217;s <tt class="docutils literal"><span class="pre">getQ</span></tt> and <tt class="docutils literal"><span class="pre">putQ</span></tt> behave like
those described for the <tt class="docutils literal"><span class="pre">waitQ</span></tt> under <a class="reference internal" href="#resources">Resources</a>, except that they are
not preemptable.  Thus, priority values are ignored when the queue
type is FIFO. Otherwise higher priority values have higher priority,
etc.</p>
<hr class="docutils" id="index-63" />
<p><strong>Example</strong>. Suppose that a random demand on an inventory is made each
day.  Each requested amount is distributed normally with a mean of 1.2
units and a standard deviation of 0.2 units.  The inventory (modelled
as an object of the Level class) is refilled by 10 units at fixed
intervals of 10 days. There are no back-orders, but a accumulated sum
of the total stock-out quantities is to be maintained.  A trace is to
be printed out each day and whenever there is a stock-out:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">normalvariate</span><span class="p">,</span> <span class="n">seed</span>
<span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Level</span><span class="p">,</span> <span class="n">Process</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">get</span><span class="p">,</span> <span class="n">initialize</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span>
        <span class="n">now</span><span class="p">,</span> <span class="n">put</span><span class="p">,</span> <span class="n">simulate</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Deliver</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">deliver</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>  <span class="c"># an &quot;offeror&quot; PEM</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="n">lead</span> <span class="o">=</span> <span class="mf">10.0</span>      <span class="c"># time between refills</span>
            <span class="n">delivery</span> <span class="o">=</span> <span class="mf">10.0</span>  <span class="c"># amount in each refill</span>
            <span class="k">yield</span> <span class="n">put</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">stock</span><span class="p">,</span> <span class="n">delivery</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;at </span><span class="si">%6.4f</span><span class="s">, add </span><span class="si">%6.4f</span><span class="s"> units, now amount = </span><span class="si">%6.4f</span><span class="s">&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="n">delivery</span><span class="p">,</span> <span class="n">stock</span><span class="o">.</span><span class="n">amount</span><span class="p">))</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">lead</span>


<span class="k">class</span> <span class="nc">Demand</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="n">stockout</span> <span class="o">=</span> <span class="mf">0.0</span>     <span class="c"># initialize initial stockout amount</span>

    <span class="k">def</span> <span class="nf">demand</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>  <span class="c"># a &quot;requester&quot; PEM</span>
        <span class="n">day</span> <span class="o">=</span> <span class="mf">1.0</span>      <span class="c"># set time-step to one day</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">day</span>
            <span class="n">dd</span> <span class="o">=</span> <span class="n">normalvariate</span><span class="p">(</span><span class="mf">1.20</span><span class="p">,</span> <span class="mf">0.20</span><span class="p">)</span>  <span class="c"># today&#39;s random demand</span>
            <span class="n">ds</span> <span class="o">=</span> <span class="n">dd</span> <span class="o">-</span> <span class="n">stock</span><span class="o">.</span><span class="n">amount</span>
            <span class="c"># excess of demand over current stock amount</span>
            <span class="k">if</span> <span class="n">dd</span> <span class="o">&gt;</span> <span class="n">stock</span><span class="o">.</span><span class="n">amount</span><span class="p">:</span>     <span class="c"># can&#39;t supply requested amount</span>
                <span class="k">yield</span> <span class="n">get</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">stock</span><span class="p">,</span> <span class="n">stock</span><span class="o">.</span><span class="n">amount</span>
                <span class="c"># supply all available amount</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">stockout</span> <span class="o">+=</span> <span class="n">ds</span>
                <span class="c"># add unsupplied demand to self.stockout</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&#39;day </span><span class="si">%6.4f</span><span class="s">, demand = </span><span class="si">%6.4f</span><span class="s">, shortfall = </span><span class="si">%6.4f</span><span class="s">&#39;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="n">dd</span><span class="p">,</span> <span class="o">-</span><span class="n">ds</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>  <span class="c"># can supply requested amount</span>
                <span class="k">yield</span> <span class="n">get</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">stock</span><span class="p">,</span> <span class="n">dd</span>
                <span class="k">print</span><span class="p">(</span><span class="s">&#39;day </span><span class="si">%6.4f</span><span class="s">, supplied </span><span class="si">%6.4f</span><span class="s">, now amount = </span><span class="si">%6.4f</span><span class="s">&#39;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="n">dd</span><span class="p">,</span> <span class="n">stock</span><span class="o">.</span><span class="n">amount</span><span class="p">))</span>


<span class="n">stock</span> <span class="o">=</span> <span class="n">Level</span><span class="p">(</span><span class="n">monitored</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>    <span class="c"># &#39;unbounded&#39; capacity and other defaults</span>

<span class="n">seed</span><span class="p">(</span><span class="mi">99999</span><span class="p">)</span>
<span class="n">initialize</span><span class="p">()</span>

<span class="n">offeror</span> <span class="o">=</span> <span class="n">Deliver</span><span class="p">()</span>
<span class="n">activate</span><span class="p">(</span><span class="n">offeror</span><span class="p">,</span> <span class="n">offeror</span><span class="o">.</span><span class="n">deliver</span><span class="p">())</span>
<span class="n">requester</span> <span class="o">=</span> <span class="n">Demand</span><span class="p">()</span>
<span class="n">activate</span><span class="p">(</span><span class="n">requester</span><span class="p">,</span> <span class="n">requester</span><span class="o">.</span><span class="n">demand</span><span class="p">())</span>

<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mf">49.9</span><span class="p">)</span>

<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">stock</span><span class="o">.</span><span class="n">bufferMon</span><span class="o">.</span><span class="n">mean</span><span class="p">(),</span> <span class="n">requester</span><span class="o">.</span><span class="n">stockout</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;Summary of results through end of day </span><span class="si">%6.4f</span><span class="s">:&#39;</span> <span class="o">%</span> <span class="nb">int</span><span class="p">(</span><span class="n">now</span><span class="p">()))</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;average stock = </span><span class="si">%6.4f</span><span class="s">, cumulative stockout = </span><span class="si">%6.4f</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">result</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>Here is the last ten day&#8217;s output from one run of this program:</p>
<div class="highlight-python"><pre>at 40.0000, add 10.0000 units, now amount = 10.0000
day 40.0000, supplied  0.7490, now amount = 9.2510
day 41.0000, supplied  1.1651, now amount = 8.0858
day 42.0000, supplied  1.1117, now amount = 6.9741
day 43.0000, supplied  1.1535, now amount = 5.8206
day 44.0000, supplied  0.9202, now amount = 4.9004
day 45.0000, supplied  0.8990, now amount = 4.0014
day 46.0000, supplied  1.1448, now amount = 2.8566
day 47.0000, supplied  1.7287, now amount = 1.1279
day 48.0000, supplied  0.9608, now amount = 0.1670
day 49.0000, demand =  0.9837, shortfall = -0.8167

Summary of results through end of day 49.0000:
average stock =  4.2720, cumulative stockout =  9.7484</pre>
</div>
<hr class="docutils" />
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="reneging">
<span id="index-64"></span><h3>Reneging<a class="headerlink" href="#reneging" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put</span></tt> can be subject to <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">reneging</a> using one of the compound
statements:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(put,self,</span></tt><em>lev,ask[,P]</em><tt class="docutils literal"><span class="pre">),</span></tt><tt class="docutils literal"><span class="pre">(hold,self,</span></tt><em>waittime</em><tt class="docutils literal"><span class="pre">)</span></tt></li>
</ul>
<p>where if the process does not acquire the amount before <em>waittime</em> is
elapsed, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution continues or</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(put,self,</span></tt><em>lev,ask[,P]</em><tt class="docutils literal"><span class="pre">),</span></tt><tt class="docutils literal"><span class="pre">(waitevent,self,</span></tt><em>events</em><tt class="docutils literal"><span class="pre">)</span></tt></li>
</ul>
<p>where if one of the SimEvents in <em>events</em> occurs before enough becomes
available, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution
continues.</p>
<p>In either case if reneging has <em>not</em> occurred the quantity will have
been put into the Level and <tt class="docutils literal"><span class="pre">self.stored(</span></tt><em>lev</em><tt class="docutils literal"><span class="pre">)</span></tt> will be
<tt class="docutils literal"><span class="pre">True</span></tt>. This must be tested immediately after the <tt class="docutils literal"><span class="pre">yield</span></tt>:</p>
<div class="highlight-python"><pre>yield (put,self,lev,ask[,P]),(&lt;reneging clause&gt;)
if self.stored(lev):
   ## process  did not renege
   . . . .
else:
   ## process reneged before being able to put into the resource</pre>
</div>
<p>The <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get</span></tt> can also be subject to <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">reneging</a> using one of the compound
statements:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,lev,ask[,P]),(hold,self,waittime)</span></tt></li>
</ul>
<p>where if the process does not acquire the amount before <em>waittime</em> is
elapsed, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution continues.</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,lev,ask[,P]),(waitevent,self,events)</span></tt></li>
</ul>
<p>where if one of the SimEvents in <em>events</em> occurs before enough becomes
available, reneging occurs, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution
continues.</p>
<p>In either case if reneging has <em>not</em> occurred <tt class="docutils literal"><span class="pre">self.got</span> <span class="pre">==</span> <span class="pre">ask</span></tt> and
<tt class="docutils literal"><span class="pre">self.acquired(lev)</span></tt> will be <tt class="docutils literal"><span class="pre">True</span></tt>. <tt class="docutils literal"><span class="pre">self.acquired(lev)</span></tt> must be called
immediately after the <tt class="docutils literal"><span class="pre">yield</span></tt>:</p>
<div class="highlight-python"><pre>yield (get,self,lev,ask[,P]),(&lt;reneging clause&gt;)
if self.acquired(lev):
   ## process  did not renege, self.got == ask
   . . . .
else:
   ## process reneged before being able to put into the resource</pre>
</div>
<p>This test removes the reneging process from the <tt class="docutils literal"><span class="pre">getQ</span></tt>.</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
<div class="section" id="stores">
<span id="index-65"></span><h2><a class="toc-backref" href="#id21">Stores</a><a class="headerlink" href="#stores" title="Permalink to this headline">¶</a></h2>
<p>The three resource facilities provided by the SimPy system are
<a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a> and <a class="reference internal" href="#stores">Stores</a>. Each models a congestion point where
process objects may have to queue up to obtain resources. This section
describes the Store type of resource facility.</p>
<p>Stores model the production and consumption of individual items of any
Python type.  Process objects can insert or remove specific items from
the list of items available in a Store.  For example, surgical
procedures (treated as process objects) require specific lists of
personnel and equipment that may be treated as the items available in
a Store type of resource facility such as a clinic or hospital. As the
items held in a Store may be of any Python type, they may in
particular be process objects, and this can be exploited to facilitate
modelling Master/Slave relationships. <em>putting</em> and <em>getting</em> may also
be subjected to reneging.</p>
<div class="section" id="defining-a-store">
<span id="index-66"></span><h3>Defining a Store<a class="headerlink" href="#defining-a-store" title="Permalink to this headline">¶</a></h3>
<p>The Store object <tt class="docutils literal"><span class="pre">sObj</span></tt> is established by a statement
like the following:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8</pre></div></td><td class="code"><div class="highlight"><pre><span class="n">sObj</span> <span class="o">=</span> <span class="n">Store</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;a_store&#39;</span><span class="p">,</span>
             <span class="n">unitName</span><span class="o">=</span><span class="s">&#39;units&#39;</span><span class="p">,</span>
             <span class="n">capacity</span><span class="o">=</span><span class="s">&#39;unbounded&#39;</span><span class="p">,</span>
             <span class="n">initialBuffered</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
             <span class="n">putQType</span><span class="o">=</span><span class="n">FIFO</span><span class="p">,</span>
             <span class="n">getQType</span><span class="o">=</span><span class="n">FIFO</span><span class="p">,</span>
             <span class="n">monitored</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
             <span class="n">monitorType</span><span class="o">=</span><span class="n">Monitor</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>where</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">name</span></tt> (string type) is a descriptive name for <em>sObj</em> (e.g.,
<tt class="docutils literal"><span class="pre">'Inventory'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">unitName</span></tt> (string type) is a descriptive name for the items
in <em>sObj</em> (e.g., <tt class="docutils literal"><span class="pre">'widgets'</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">capacity</span></tt> (positive integer) is the maximum number of
individual items that can be held in <em>sObj</em>.
The default value is set to <tt class="docutils literal"><span class="pre">'unbounded'</span></tt> which is
interpreted as <tt class="docutils literal"><span class="pre">sys.maxint</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">initialBuffered</span></tt> (a list of individual items) is <em>sObj</em>&#8216;s initial content.</li>
<li><tt class="docutils literal"><span class="pre">putQType</span></tt> (<tt class="docutils literal"><span class="pre">FIFO</span></tt> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt>) is the (producer) queue
discipline.</li>
<li><tt class="docutils literal"><span class="pre">getQType</span></tt> (<tt class="docutils literal"><span class="pre">FIFO</span></tt> or <tt class="docutils literal"><span class="pre">PriorityQ</span></tt>) is the (consumer) queue discipline.</li>
<li><tt class="docutils literal"><span class="pre">monitored</span></tt> (boolean) specifies whether <em>sObj</em>&#8216;s queues and contents
are to be recorded.</li>
<li><tt class="docutils literal"><span class="pre">monitorType</span></tt> (<tt class="docutils literal"><span class="pre">Monitor</span></tt> or <tt class="docutils literal"><span class="pre">Tally</span></tt>) specifies the type of <a class="reference internal" href="#recording-simulation-results">Recorder</a>
to be used. Defaults to <tt class="docutils literal"><span class="pre">Monitor</span></tt>.</li>
</ul>
</div></blockquote>
<p>The Store object <em>sObj</em> also has the following additional attributes:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">sObj.theBuffer</span></tt> is a queue (list) of the individual items in <em>sObj</em>.
This list is in FIFO order unless the user stores them in a particular
order (see <a class="reference internal" href="#storing-objects-in-an-order">Storing objects in an order</a> , below). It is
read-only and not directly changeable by the user.</li>
<li><tt class="docutils literal"><span class="pre">sObj.nrBuffered</span></tt> is the current number of objects in <em>sObj</em>.
This is read-only and not directly changeable by the user.</li>
<li><tt class="docutils literal"><span class="pre">sObj.putQ</span></tt> is the queue of processes waiting to add items to <em>sObj</em>,
so that <tt class="docutils literal"><span class="pre">len(sObj.putQ)</span></tt> is the number of processes waiting to add items.</li>
<li><tt class="docutils literal"><span class="pre">sObj.getQ</span></tt> is the queue of processes waiting to get items from
<em>sObj</em>, so that <tt class="docutils literal"><span class="pre">len(sObj.getQ)</span></tt> is the number of processes waiting to
get items.</li>
<li>If <tt class="docutils literal"><span class="pre">sObj.monitored</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt> then the queues are to be recorded.
In this case <tt class="docutils literal"><span class="pre">sObj.putQMon</span></tt>,
<tt class="docutils literal"><span class="pre">sObj.getQMon</span></tt>, and <tt class="docutils literal"><span class="pre">sObj.bufferMon</span></tt> exist.</li>
<li><tt class="docutils literal"><span class="pre">sObj.putQMon</span></tt> is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">sObj.putQ</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">sObj.getQMon</span></tt> is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">sObj.getQ</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">sObj.bufferMon</span></tt>  is a <a class="reference internal" href="#recording-simulation-results">Recorder</a> observing <tt class="docutils literal"><span class="pre">sObj.nrBuffered</span></tt>.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="putting-objects-into-a-store">
<span id="index-67"></span><h3>Putting objects into a Store<a class="headerlink" href="#putting-objects-into-a-store" title="Permalink to this headline">¶</a></h3>
<p>Processes can request items from a Store and the same or
other processes can offer items to it. First look at the simpler of
these operations, the <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put</span></tt>.</p>
<p>A process, the <em>offerer</em>, which is usually but not necessarily
different from the <em>requester</em>, can offer a list of
items to <em>sObj</em> by a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put</span></tt> statement:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put,self,sObj,give[,P]</span></tt></li>
</ul>
<p>Here <tt class="docutils literal"><span class="pre">give</span></tt> is a list of any Python objects. If this statement would
lead to an overflow (that is, <tt class="docutils literal"><span class="pre">sObj.nrBuffered</span> <span class="pre">+</span> <span class="pre">len(give)</span> <span class="pre">&gt;</span>
<span class="pre">sObj.capacity</span></tt>) the putting process is passivated and queued (in
<tt class="docutils literal"><span class="pre">sObj.putQ</span></tt>) until there is sufficient room. <em>P</em> is an optional
priority value (real or integer).</p>
<p>The ordering of processes in a Store&#8217;s putQ and getQ behave like those
described for the waitQ under <a class="reference internal" href="#resources">Resources</a> , except that they are not
preemptable.  Thus, priority values are ignored when the queue type is
FIFO.  Otherwise higher priority values indicate higher priority, etc.</p>
<p>The items in <em>sObj</em> are stored in the form of a queue called
<tt class="docutils literal"><span class="pre">sObj.theBuffer</span></tt>, which is in FIFO order unless the user has
arranged to sort them into a particular order (see <a class="reference internal" href="#storing-objects-in-an-order">Storing objects in
an order</a> below).</p>
</div>
<div class="section" id="getting-objects-from-a-store">
<span id="index-68"></span><h3>Getting objects from  a Store<a class="headerlink" href="#getting-objects-from-a-store" title="Permalink to this headline">¶</a></h3>
<p>There are two ways of getting objects from a Store. A process, the
<em>requester</em>, can either extract the first <tt class="docutils literal"><span class="pre">n</span></tt> objects from <em>sObj</em> or a list
of items chosen by a <em>filter function</em>.</p>
<p>Getting <em>n</em> items is achieved by the following statement:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get,self,sObj,n</span> <span class="pre">[,P]</span></tt></li>
</ul>
<p>Here <em>n</em> must be a positive integer and <em>P</em> is an optional priority
value (real or integer).  If <em>sObj</em> does not currently hold enough
objects to satisfy this request (that is, <tt class="docutils literal"><span class="pre">n</span> <span class="pre">&gt;</span> <span class="pre">sObj.nrBuffered</span></tt>)
then the requesting process is passivated and queued (in
<tt class="docutils literal"><span class="pre">sObj.getQ</span></tt>). Subject to the priority ordering, it will be
reactivated when the request can be satisfied.</p>
<p>The retrieved objects are returned in the list attribute <tt class="docutils literal"><span class="pre">got</span></tt> of
the requesting process.</p>
<p><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get</span></tt> requests with a numerical parameter are honored in
priority/FIFO order. Thus, if
there are two processes in the Store&#8217;s <tt class="docutils literal"><span class="pre">getQ</span></tt>, with the first requesting
two items and the second one, the second process gets the requested item
only after the first process has been given its two items.</p>
</div>
<div class="section" id="using-the-get-filter-function">
<span id="index-69"></span><h3>Using the get filter function<a class="headerlink" href="#using-the-get-filter-function" title="Permalink to this headline">¶</a></h3>
<p>The second method is to get a list of items chosen by a <em>filter
function</em>, written by the user.</p>
<p>The command, using filter function <em>ffn</em> is as follows:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get,self,sObj,ffn</span> <span class="pre">[,P]</span></tt></li>
</ul>
<p>The user provides a filter function that has a single list argument and
returns a list. The argument represents the buffer of the Store. The
function must search through the objects in the buffer and return a
sub-list of those that satisfy the requirement.</p>
<hr class="docutils" id="index-70" />
<p><strong>Example</strong> The filter function <tt class="docutils literal"><span class="pre">allweight</span></tt>, shown below, is an example
of such a filter. The argument, <tt class="docutils literal"><span class="pre">buff</span></tt>, will be automatically
replaced in the execution of
<tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get,self,store,allweight</span></tt> by the buffer of the Store. In this
example the objects in the Store are assumed to have <tt class="docutils literal"><span class="pre">weight</span></tt>
attributes. The function <tt class="docutils literal"><span class="pre">allweight</span></tt> selects all those that have a
weight attribute over a value <tt class="docutils literal"><span class="pre">W</span></tt> and returns these as a list. The
list appears to the calling process as <tt class="docutils literal"><span class="pre">self.got</span></tt>:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">allweight</span><span class="p">(</span><span class="n">buff</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;filter: get all items with .weight &gt;=W from store&quot;&quot;&quot;</span>
    <span class="n">result</span><span class="o">=</span><span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">buff</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">weight</span><span class="o">&gt;=</span><span class="n">W</span><span class="p">:</span>
            <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">result</span>
</pre></div>
</td></tr></table></div>
<p>This might be used as follows:</p>
<div class="highlight-python"><pre>yield get,self,sObj,allweight [,P]</pre>
</div>
<p>The retrieved objects are returned in the list attribute <tt class="docutils literal"><span class="pre">got</span></tt> of
the requesting process.</p>
<hr class="docutils" />
<p><strong>Note:</strong> <em>``yield get``</em> requests with a filter function parameter
are not necessarily honored in priority/FIFO order, but rather
according to the filter function. An example: There are two processes
in the Store&#8217;s <tt class="docutils literal"><span class="pre">getQ</span></tt>, with the first requesting an item with a
<em>weight</em> attribute less than 2 kilograms and the second one requesting
one with a <tt class="docutils literal"><span class="pre">weight</span></tt> attribute less than 3 kilograms. If there is an
item in the Store&#8217;s buffer with a <tt class="docutils literal"><span class="pre">weight</span></tt> attribute between 2 and 3
and none with an attribute of less than 2, the second <tt class="docutils literal"><span class="pre">get</span></tt> requester
gets unblocked before the first one. Effectively, the SimPy run time
system runs through all processes in the <tt class="docutils literal"><span class="pre">getQ</span></tt> in sequence and
tests their filter functions as long as there are still items in the
Store&#8217;s buffer.</p>
<hr class="docutils" />
<p><strong>Example</strong> The following program illustrates the use of a Store to
model the production and consumption of &#8220;widgets&#8221;. The widgets are
distinguished by their weight:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Lister</span><span class="p">,</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Store</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">get</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span>
    <span class="n">initialize</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">put</span><span class="p">,</span> <span class="n">simulate</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ProducerD</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">produce</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>  <span class="c"># the ProducerD PEM</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">put</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="p">[</span><span class="n">Widget</span><span class="p">(</span><span class="mi">9</span><span class="p">),</span> <span class="n">Widget</span><span class="p">(</span><span class="mi">7</span><span class="p">)]</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mi">10</span>


<span class="k">class</span> <span class="nc">ConsumerD</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">consume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>  <span class="c"># the ConsumerD PEM</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="n">toGet</span> <span class="o">=</span> <span class="mi">3</span>
            <span class="k">yield</span> <span class="n">get</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="n">toGet</span>
            <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">got</span><span class="p">)</span> <span class="o">==</span> <span class="n">toGet</span>
            <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> Get widget weights </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">now</span><span class="p">(),</span>
                    <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">weight</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">got</span><span class="p">]))</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mi">11</span>


<span class="k">class</span> <span class="nc">Widget</span><span class="p">(</span><span class="n">Lister</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">weight</span>


<span class="n">widgbuf</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
    <span class="n">widgbuf</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Widget</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>

<span class="n">initialize</span><span class="p">()</span>

<span class="n">buf</span> <span class="o">=</span> <span class="n">Store</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">initialBuffered</span><span class="o">=</span><span class="n">widgbuf</span><span class="p">,</span> <span class="n">monitored</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>  <span class="c"># define and activate 3 producer objects</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">ProducerD</span><span class="p">()</span>
    <span class="n">activate</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">produce</span><span class="p">())</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>  <span class="c"># define and activate 3 consumer objects</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">ConsumerD</span><span class="p">()</span>
    <span class="n">activate</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">consume</span><span class="p">())</span>

<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s">&#39;LenBuffer: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">buf</span><span class="o">.</span><span class="n">bufferMon</span><span class="p">)</span>  <span class="c"># length of buffer</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;getQ: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">buf</span><span class="o">.</span><span class="n">getQMon</span><span class="p">)</span>         <span class="c"># length of getQ</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;putQ </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">buf</span><span class="o">.</span><span class="n">putQMon</span><span class="p">)</span>          <span class="c"># length of putQ</span>
</pre></div>
</td></tr></table></div>
<p>This program produces the following outputs
(some lines may be formatted differently):</p>
<div class="highlight-python"><pre>0 Got widget weights [5, 5, 5]
0 Got widget weights [5, 5, 5]
0 Got widget weights [5, 5, 5]
11 Got widget weights [5, 9, 7]
11 Got widget weights [9, 7, 9]
11 Got widget weights [7, 9, 7]
22 Got widget weights [9, 7, 9]
22 Got widget weights [7, 9, 7]
22 Got widget weights [9, 7, 9]
33 Got widget weights [7, 9, 7]
33 Got widget weights [9, 7, 9]
40 Got widget weights [7, 9, 7]
44 Got widget weights [9, 7, 9]
50 Got widget weights [7, 9, 7]
LenBuffer: [[0, 10], [0, 7], [0, 9], [0, 11], [0, 8], [0, 10], [0, 7],
    [10, 9], [10, 11], [11, 8], [11, 10], [11, 7], [11, 4],
    [20, 6], [20, 8], [21, 10], [22, 7], [22, 4], [22, 1],
    [30, 3], [30, 5], [31, 7], [33, 4], [33, 1],
    [40, 3], [40, 0], [40, 2], [41, 4], [44, 1], [50, 3], [50, 0], [50, 2]]
getQ: [[0, 0], [33, 1], [40, 0], [44, 1], [50, 0]]
putQ [[0, 0], [0, 1], [0, 2], [0, 3], [0, 2], [0, 1], [0, 0], [10, 1],\
    [11, 0]]</pre>
</div>
<hr class="docutils" />
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="index-71">
<span id="id11"></span><h3>Reneging<a class="headerlink" href="#index-71" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">put</span></tt> can be subject to <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">reneging</a> using one of the compound
statements:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(put,self,sObj,give</span> <span class="pre">[,P]),(hold,self,waittime)</span></tt></li>
</ul>
<p>where if the process cannot put the list of objects in <em>give</em> before <em>waittime</em> is
elapsed, the offerer leaves the <tt class="docutils literal"><span class="pre">putQ</span></tt> and its execution continues or</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(put,self,sObj,give</span> <span class="pre">[,P]),(waitevent,self,events)</span></tt></li>
</ul>
<p>where if one of the SimEvents in <em>events</em> occurs before it can put the
list of objects in <em>give</em> the offerer leaves the <tt class="docutils literal"><span class="pre">putQ</span></tt> and its
execution continues.</p>
<p>In either case if reneging has <em>not</em> occurred the list of objects in
<em>give</em> will have been put into the Store and <tt class="docutils literal"><span class="pre">self.stored(Sobj)</span></tt> will
be <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>The mandatory pattern for a <tt class="docutils literal"><span class="pre">put</span></tt> with reneging is:</p>
<div class="highlight-python"><pre>yield (put,self,sObj,give [,P]),(&lt;reneging clause&gt;)
if self.stored(sObj):
   ## process  did not renege
   . . . .
else:
   ## process reneged before being able to put into the resource</pre>
</div>
<p>This is so because <tt class="docutils literal"><span class="pre">self.stored(</span> <span class="pre">)</span></tt> not only tests for reneging, but
it also cleanly removes a reneging process from the <tt class="docutils literal"><span class="pre">putQ</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get</span></tt> can be subject to similar <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">reneging</a> using one of the compound
statements:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,sObj,n</span> <span class="pre">[,P]),(hold,self,waittime)</span></tt></li>
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,sObj,ffn</span> <span class="pre">[,P]),(hold,self,waittime)</span></tt></li>
</ul>
<p>where if the process does not acquire the amount before <em>waittime</em> is
elapsed, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution continues.</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,sObj,n</span> <span class="pre">[,P]),(waitevent,self,events)</span></tt></li>
<li><tt class="docutils literal"><span class="pre">yield</span> <span class="pre">(get,self,sObj,ffn</span> <span class="pre">[,P]),(waitevent,self,events)</span></tt></li>
</ul>
<p>where if one of the SimEvents in <em>events</em> occurs before enough becomes
available, reneging occurs, the offerer leaves the <tt class="docutils literal"><span class="pre">waitQ</span></tt> and its execution
continues.</p>
<p>In either case if reneging has <em>not</em> occurred <tt class="docutils literal"><span class="pre">self.got</span></tt> contains
the list of retrieved objects and <tt class="docutils literal"><span class="pre">self.acquired(Sobj)</span></tt> will be
<tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<p>The mandatory pattern for a <tt class="docutils literal"><span class="pre">get</span></tt> with reneging is:</p>
<div class="highlight-python"><pre>yield (get,self,lev,sObj,&lt;n or ffn&gt; [,P]),(&lt;reneging clause&gt;)
if self.acquired(sObj):
   ## process  did not renege,
   . . . .
else:
   ## process reneged before being able to put into the resource</pre>
</div>
<p>This is so because <tt class="docutils literal"><span class="pre">self.acquired(</span> <span class="pre">)</span></tt> not only tests for reneging, but
it also cleanly removes a reneging process from the <tt class="docutils literal"><span class="pre">getQ</span></tt>.</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="storing-objects-in-an-order">
<span id="index-72"></span><h3>Storing objects in an order<a class="headerlink" href="#storing-objects-in-an-order" title="Permalink to this headline">¶</a></h3>
<p>The contents of a Store instance are listed in a queue.  By default,
this list is kept in FIFO order.  However, the list can be kept in a
user-defined order. You do this by defining a function for reordering
the list and adding it to the Store instance for which you want to
change the list order. Subsequently, the SimPy system will
automatically call that function after any addition (<tt class="docutils literal"><span class="pre">put</span></tt>) to the
queue.</p>
<hr class="docutils" id="index-73" />
<p><strong>Example</strong></p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Parcel</span><span class="p">:</span>
     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">weight</span><span class="p">):</span>
         <span class="bp">self</span><span class="o">.</span><span class="n">weight</span><span class="o">=</span><span class="n">weight</span>

<span class="n">lightFirst</span><span class="o">=</span><span class="n">Store</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">getLightFirst</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">par</span><span class="p">):</span>
     <span class="sd">&quot;&quot;&quot;Lighter parcels to front of queue&quot;&quot;&quot;</span>
     <span class="n">tmplist</span><span class="o">=</span><span class="p">[(</span><span class="n">x</span><span class="o">.</span><span class="n">weight</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">par</span><span class="p">]</span>
     <span class="n">tmplist</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
     <span class="k">return</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="ow">in</span> <span class="n">tmplist</span><span class="p">]</span>

<span class="n">lightFirst</span><span class="o">.</span><span class="n">addSort</span><span class="p">(</span><span class="n">getLightFirst</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>Now any <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">get</span></tt> will get the lightest parcel in <tt class="docutils literal"><span class="pre">lightFirst</span></tt>&#8216;s
queue.</p>
<hr class="docutils" />
<p>The <tt class="docutils literal"><span class="pre">par</span></tt> parameter is automatically given the Store&#8217;s buffer list as value
when the SimPy run time system calls the re-ordering function.</p>
<p><em>&lt;aStore&gt;.addSort(&lt;reorderFunction&gt;)</em> adds a re-order function to &lt;aStore&gt;.</p>
<p>Note that such function only changes the sorting order of the Store instance,
NOT of the Store class.</p>
</div>
<div class="section" id="master-slave-modelling-with-a-store">
<span id="index-74"></span><h3>Master/Slave modelling with a Store<a class="headerlink" href="#master-slave-modelling-with-a-store" title="Permalink to this headline">¶</a></h3>
<p>The items in a <tt class="docutils literal"><span class="pre">Store</span></tt> can be of any Python type. In particular, they
may be SimPy processes. This can be used to model
a Master/Slave situation &#8211; an asymmetrical cooperation between two or
more processes, with one process (the Master) being in charge of the
cooperation.</p>
<p>The consumer (Master) requests one or more Slaves to be added to the
Store&#8217;s contents by the Producer (which may be the same process as the
Slave).  For Master/Slave cooperation, the Slave has to be passivated
(by a <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">passivate</span></tt> or <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">waitevent</span></tt> statement) after it is
<tt class="docutils literal"><span class="pre">put</span></tt> and reactivated when it is retrieved and finished with. As this
is NOT done automatically by the <tt class="docutils literal"><span class="pre">Store</span></tt>, the Master has to signal the
end of the cooperation. This Master/Slave pattern results in the slave process&#8217;
life-cycle having a hole between the slave process arrival and its departure
after having been served.</p>
<hr class="docutils" id="index-75" />
<p><strong>Example</strong> Cars arrive randomly at a car wash and add themselves to
the <tt class="docutils literal"><span class="pre">waitingCars</span></tt> queue. They wait (passively) for a <tt class="docutils literal"><span class="pre">doneSignal</span></tt>.
There are two <tt class="docutils literal"><span class="pre">Carwash</span></tt> washers. These <tt class="docutils literal"><span class="pre">get</span></tt> a car, if one is
available, wash it, and then send the <tt class="docutils literal"><span class="pre">doneSignal</span></tt> to reactivate
it. We elect to model the <tt class="docutils literal"><span class="pre">Carwash</span></tt> as Master and the <tt class="docutils literal"><span class="pre">Cars</span></tt> as
slaves.</p>
<p>Four cars are put into the <tt class="docutils literal"><span class="pre">waiting</span></tt> list and these make up the
initial set of cars waiting for service. Additional cars are generated
randomly by the <tt class="docutils literal"><span class="pre">CarGenerator</span></tt> process. Each car <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">puts</span></tt> itself
onto the <tt class="docutils literal"><span class="pre">waitingCars</span></tt> <tt class="docutils literal"><span class="pre">Store</span></tt> and immediately passivates itself by
waiting for a <tt class="docutils literal"><span class="pre">doneSignal</span></tt> from a car washer. The car washers cycle
round <tt class="docutils literal"><span class="pre">getting</span></tt> the next car on the queue, washing it and then sending
a <tt class="docutils literal"><span class="pre">doneSignal</span></tt> to it when it has finished:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Process</span><span class="p">,</span> <span class="n">SimEvent</span><span class="p">,</span> <span class="n">Store</span><span class="p">,</span> <span class="n">activate</span><span class="p">,</span> <span class="n">get</span><span class="p">,</span>
        <span class="n">initialize</span><span class="p">,</span> <span class="n">hold</span><span class="p">,</span> <span class="n">now</span><span class="p">,</span> <span class="n">put</span><span class="p">,</span> <span class="n">simulate</span><span class="p">,</span> <span class="n">waitevent</span><span class="p">)</span>
<span class="sd">&quot;&quot;&quot;Carwash is master&quot;&quot;&quot;</span>


<span class="k">class</span> <span class="nc">Carwash</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Carwash is master&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lifecycle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">get</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">waitingCars</span><span class="p">,</span> <span class="mi">1</span>
            <span class="n">carBeingWashed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">got</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">washtime</span>
            <span class="n">carBeingWashed</span><span class="o">.</span><span class="n">doneSignal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Car is slave&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">Process</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">doneSignal</span> <span class="o">=</span> <span class="n">SimEvent</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">lifecycle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">put</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">waitingCars</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="p">]</span>
        <span class="k">yield</span> <span class="n">waitevent</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">doneSignal</span>
        <span class="n">whichWash</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">doneSignal</span><span class="o">.</span><span class="n">signalparam</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> car </span><span class="si">%s</span><span class="s"> done by </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">now</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">whichWash</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">CarGenerator</span><span class="p">(</span><span class="n">Process</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">hold</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="mi">2</span>
            <span class="n">c</span> <span class="o">=</span> <span class="n">Car</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span>
            <span class="n">activate</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">lifecycle</span><span class="p">())</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>

<span class="n">washtime</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">initialize</span><span class="p">()</span>

<span class="c"># put four cars into the queue of waiting cars</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">Car</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;</span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="o">-</span><span class="n">j</span><span class="p">)</span>
        <span class="n">activate</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">lifecycle</span><span class="p">())</span>

<span class="n">waitingCars</span> <span class="o">=</span> <span class="n">Store</span><span class="p">(</span><span class="n">capacity</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
        <span class="n">cw</span> <span class="o">=</span> <span class="n">Carwash</span><span class="p">(</span><span class="s">&#39;Carwash </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span>
        <span class="n">activate</span><span class="p">(</span><span class="n">cw</span><span class="p">,</span> <span class="n">cw</span><span class="o">.</span><span class="n">lifecycle</span><span class="p">())</span>

<span class="n">cg</span> <span class="o">=</span> <span class="n">CarGenerator</span><span class="p">()</span>
<span class="n">activate</span><span class="p">(</span><span class="n">cg</span><span class="p">,</span> <span class="n">cg</span><span class="o">.</span><span class="n">generate</span><span class="p">())</span>
<span class="n">simulate</span><span class="p">(</span><span class="n">until</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">&#39;waitingCars </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">waitingCars</span><span class="o">.</span><span class="n">theBuffer</span><span class="p">])</span>
</pre></div>
</td></tr></table></div>
<p>The output of this program, running to time 30, is:</p>
<div class="highlight-python"><pre>5 car -1 done by Carwash 0
5 car -2 done by Carwash 1
10 car -3 done by Carwash 0
10 car -4 done by Carwash 1
15 car 0 done by Carwash 0
15 car 1 done by Carwash 1
20 car 2 done by Carwash 0
20 car 3 done by Carwash 1
25 car 4 done by Carwash 0
25 car 5 done by Carwash 1
30 car 6 done by Carwash 0
30 car 7 done by Carwash 1
waitingCars [10, 11, 12, 13, 14]</pre>
</div>
<p>It is also possible to model this car wash with the cars as Master
and the <tt class="docutils literal"><span class="pre">Carwash</span></tt> as Slaves.</p>
<hr class="docutils" />
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
<div class="section" id="random-number-generation">
<span id="index-76"></span><h2><a class="toc-backref" href="#id22">Random Number Generation</a><a class="headerlink" href="#random-number-generation" title="Permalink to this headline">¶</a></h2>
<p>Simulations usually need random numbers. As SimPy does not supply
random number generators of its own, users need to import them from
some other source.  Perhaps the most convenient source is the standard
<a class="reference external" href="http://www.python.org/doc/current/lib/module-random.html">Python random module</a>.  It can generate random variates from the
following continuous distributions: uniform, beta, exponential, gamma,
normal, log-normal, weibull, and vonMises.  It can also generate random
variates from some discrete distributions. Consult the module&#8217;s
documentation for details.  (Excellent brief descriptions of these
distributions, and many others, can be found in the <a class="reference external" href="http://www.wikipedia.com/">Wikipedia</a>.)</p>
<p>Python&#8217;s <tt class="docutils literal"><span class="pre">random</span></tt> module can be used in two ways: you can import the
methods directly or you can import the <tt class="docutils literal"><span class="pre">Random</span></tt> class and make your
own random objects. In the second method, each object gives a
different random number sequence, thus providing multiple random
streams as in Simscript and ModSim.</p>
<p>Here the first method is described (and minimally at that). A single
pseudo-random sequence is used for all calls. You <tt class="docutils literal"><span class="pre">import</span></tt> the
methods you need from the <tt class="docutils literal"><span class="pre">random</span></tt> module. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">seed</span><span class="p">,</span> <span class="n">random</span><span class="p">,</span> <span class="n">expovariate</span><span class="p">,</span> <span class="n">normalvariate</span>
</pre></div>
</div>
<p>In simulation it is good practice to set the initial <tt class="docutils literal"><span class="pre">seed</span></tt> for the
pseudo-random sequence at the start of each run. Then you have control
over the random numbers used. Replications and comparisons are
easier and, together with variance reduction techniques, can provide more
accurate estimates. In the following code snippet we set the initial
seed to 333555.  <tt class="docutils literal"><span class="pre">X</span></tt> and <tt class="docutils literal"><span class="pre">Y</span></tt> are pseudo-random variates from the
two distributions. Both distributions have the same mean:</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">seed</span><span class="p">,</span> <span class="n">expovariate</span><span class="p">,</span> <span class="n">normalvariate</span>

<span class="n">seed</span><span class="p">(</span><span class="mi">333555</span><span class="p">)</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">expovariate</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="n">Y</span> <span class="o">=</span> <span class="n">normalvariate</span><span class="p">(</span><span class="mf">10.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="recording-simulation-results">
<span id="index-77"></span><h2><a class="toc-backref" href="#id23">Recording Simulation Results</a><a class="headerlink" href="#recording-simulation-results" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">Tally</span></tt> and <tt class="docutils literal"><span class="pre">Monitor</span></tt> class objects  enable us to observe a single
variable of interest and to return a simple data summary either during
or at the completion of a simulation run.</p>
<p>Both use the <tt class="docutils literal"><span class="pre">observe</span></tt> method to record data on one variable. For
example we might use a Monitor object to record the waiting times for
a sequence of customers and another to record the total number of
customers in the shop. In a discrete-event system the number of
customers changes only at arrival or departure events and it is at
those events that the waiting times and number in the shop must be
observed. Monitors and Tallys provide elementary statistics useful
either alone or as the start of a more sophisticated statistical
analysis and have proved invaluable in many simulations.</p>
<p>A few more tools associated with recording results are:</p>
<ul class="simple">
<li>All Monitors are registered automatically in the global list
variable <tt class="docutils literal"><span class="pre">allMonitors</span></tt> and all Tallys in variable
<tt class="docutils literal"><span class="pre">allTallies</span></tt>. When a simulation is completed results can easily be
tabulated and summarized using these lists.</li>
<li>The function <tt class="docutils literal"><span class="pre">startCollection()</span></tt> can be called to initialize Monitors
and Tallys at a certain simulation time. This is helpful when a
simulation needs a &#8216;warmup&#8217; period to achieve steady state before
measurements are started.</li>
</ul>
<div class="section" id="defining-tallys-and-monitors">
<h3>Defining Tallys and Monitors<a class="headerlink" href="#defining-tallys-and-monitors" title="Permalink to this headline">¶</a></h3>
<p>The &#8216;&#8217;Tally&#8217;&#8217; class records enough information (such as sums and sums of
squares) while the simulation runs to return simple data summaries.
This has the advantage of speed and low memory use. Tallys can also
furnish data for a histogram. However, they do not preserve a
time-series usable in more advanced statistical analysis. When a Tally
is defined it is automatically added  to the global list <tt class="docutils literal"><span class="pre">allTallies</span></tt>.</p>
<p id="index-78">To define a new Tally object:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">m=Tally(name='a_Tally',</span> <span class="pre">ylab='y',</span> <span class="pre">tlab='t')</span></tt></li>
</ul>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">name</span></tt> is a descriptive name for the tally object (default=&#8217;<tt class="docutils literal"><span class="pre">a_Tally</span></tt>&#8216; ).</li>
<li><tt class="docutils literal"><span class="pre">ylab</span></tt> and <tt class="docutils literal"><span class="pre">tlab</span></tt> are descriptive labels used by the <tt class="docutils literal"><span class="pre">SimPy.SimPlot</span></tt>
package when plotting graphs of the recorded data. They
default to <tt class="docutils literal"><span class="pre">'y'</span></tt> and <tt class="docutils literal"><span class="pre">'t'</span></tt>, respectively. (If a <a class="reference internal" href="#histograms">histogram</a> is
required the method <tt class="docutils literal"><span class="pre">setHistogram</span></tt> must be called before recording
starts).</li>
</ul>
</div></blockquote>
<p>The <tt class="docutils literal"><span class="pre">Monitor</span></tt> class preserves a complete time-series of the observed
data values, <em>y</em>, and their associated times, <em>t</em>. It calculates the
data summaries using these series only when they are needed. It is
slower and uses more memory than <tt class="docutils literal"><span class="pre">Tally</span></tt>. In long simulations its
memory demands may be a disadvantage. When a Monitor is defined it is
automatically added to the global list <tt class="docutils literal"><span class="pre">allMonitors</span></tt>.</p>
<p id="index-79">To define a new Monitor object:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">m=Monitor(name='a_Monitor',</span> <span class="pre">ylab='y',</span> <span class="pre">tlab='t')</span></tt></li>
</ul>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">name</span></tt> is a descriptive name for the Monitor object (default=&#8217;<tt class="docutils literal"><span class="pre">a_Monitor</span></tt>&#8216;).</li>
<li><tt class="docutils literal"><span class="pre">ylab</span></tt> and <tt class="docutils literal"><span class="pre">tlab</span></tt> are descriptive labels used by the <tt class="docutils literal"><span class="pre">SimPy.SimPlot</span></tt>
package when plotting graphs of the recorded data. They
default to <tt class="docutils literal"><span class="pre">'y'</span></tt> and <tt class="docutils literal"><span class="pre">'t'</span></tt>, respectively.
(A <a class="reference internal" href="#histograms">histogram</a> can be
requested at any time).</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="observing-data">
<span id="index-80"></span><h3>Observing data<a class="headerlink" href="#observing-data" title="Permalink to this headline">¶</a></h3>
<p>Both Tallys and Monitors use the <tt class="docutils literal"><span class="pre">observe</span></tt> method to record data.
Here and in the next section, <em>r</em> is either a Tally or a Monitor object:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">r.observe(y</span> <span class="pre">[,t])</span></tt> records the current value of the variable, <tt class="docutils literal"><span class="pre">y</span></tt>
and time <em>t</em> (or the current time, <tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)</span></tt>, if <em>t</em> is missing). A
Monitor retains the two values as a sub-list <tt class="docutils literal"><span class="pre">[t,y]</span></tt>. A Tally
uses them to update the accumulated statistics.</p>
<p>To assure that time averages are calculated correctly <tt class="docutils literal"><span class="pre">observe</span></tt>
should be called immediately <em>after</em> a change in the variable. For
example, if we are using Monitor <em>r</em> to record the number <em>N</em>
of jobs in a system, the correct sequence of commands on an arrival
is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">N</span> <span class="o">=</span> <span class="n">N</span><span class="o">+</span><span class="mi">1</span>      <span class="c"># FIRST, increment the number of jobs</span>
<span class="n">r</span><span class="o">.</span><span class="n">observe</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="c"># THEN observe the new value of N using r</span>
</pre></div>
</div>
</li>
</ul>
<p>The recording of data can be <tt class="docutils literal"><span class="pre">reset</span></tt> to start at any time in the
simulation:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">r.reset([t])</span></tt> resets the observations. The recorded data is
re-initialized, and the observation starting time is set to <em>t</em>,
or to the current simulation time, <tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)</span></tt>, if <em>t</em>
is missing.</li>
</ul>
</div>
<div class="section" id="data-summaries">
<span id="index-81"></span><h3>Data summaries<a class="headerlink" href="#data-summaries" title="Permalink to this headline">¶</a></h3>
<p>The following simple data summaries can be obtained from either
Monitors or Tallys at any time during or after the simulation run:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">r.count(</span> <span class="pre">)</span></tt>, the current number of observations. (If <em>r</em> is a
Monitor this is the same as <tt class="docutils literal"><span class="pre">len(r)</span></tt>).</li>
<li><tt class="docutils literal"><span class="pre">r.total(</span> <span class="pre">)</span></tt>, the sum of the <tt class="docutils literal"><span class="pre">y</span></tt> values</li>
</ul>
<ul id="index-82">
<li><p class="first"><tt class="docutils literal"><span class="pre">r.mean(</span> <span class="pre">)</span></tt>, the simple average of the observed <tt class="docutils literal"><span class="pre">y</span></tt> values,
ignoring the times at which they were made.  This is <tt class="docutils literal"><span class="pre">r.total(</span> <span class="pre">)/N</span></tt>
where <tt class="docutils literal"><span class="pre">N=r.count(</span> <span class="pre">)</span></tt>. (If there are no observations, the message:
&#8220;SimPy: No observations for mean&#8221; is printed). See
<a class="reference internal" href="#recording-resource-queue-lengths">Recording Resource queue lengths</a> for the difference between
the simple or numerical average and the time-average.</p>
<div class="figure align-center" style="width: 400">
<img alt="Standard mean value" src="../_images/Mon004.png" />
<p class="caption"><tt class="docutils literal"><span class="pre">r.mean</span></tt> is the simple average of the <tt class="docutils literal"><span class="pre">y</span></tt> values observed.</p>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">r.var(</span> <span class="pre">)</span></tt> the <em>sample</em> variance of the observations, ignoring the
times at which they were made. If an unbiased estimate of the
<em>population</em> variance is desired, the sample variance should be
multiplied by <em>n/(n-1)</em>, where <em>n = r.count( )</em>.  In either case the
standard deviation is, of course, the square-root of the variance
(If there are no observations, the message: &#8220;SimPy: No observations
for sample variance&#8221; is printed).</p>
</li>
</ul>
<ul id="index-83">
<li><p class="first"><tt class="docutils literal"><span class="pre">r.timeAverage([t])</span></tt> the time-weighted average of <tt class="docutils literal"><span class="pre">y</span></tt>,
calculated from time 0 (or the last time <tt class="docutils literal"><span class="pre">r.reset([t])</span></tt> was
called) to time <em>t</em> (or to the current simulation time, <tt class="docutils literal"><span class="pre">now(</span> <span class="pre">)</span></tt>,
if <em>t</em> is missing).  This is determined from the area under the
graph shown in the figure, divided by the total time of observation.
For accurate time-average results <tt class="docutils literal"><span class="pre">y</span></tt> most be piecewise constant
and <tt class="docutils literal"><span class="pre">observed</span></tt> just after each change in its value. (If there are
no observations, the message &#8220;SimPy: No observations for
timeAverage&#8221; is printed. If no time has elapsed, the message &#8220;SimPy:
No elapsed time for timeAverage&#8221; is printed).</p>
<div class="figure align-center" style="width: 400">
<img alt="Time Average" src="../_images/Mon005.png" />
<p class="caption"><tt class="docutils literal"><span class="pre">r.timeAverage(</span> <span class="pre">)</span></tt> is the time-weighted average of the observed
<tt class="docutils literal"><span class="pre">y</span></tt> values. Each <tt class="docutils literal"><span class="pre">y</span></tt> value is weighted by the time for which it
exists. The average is the area under the above curve divided by the total
time, <em>t</em>.</p>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">r.timeVariance([t])</span></tt> the time-weighted variance of the <tt class="docutils literal"><span class="pre">y</span></tt>
values calculated from time 0 (or the last time <tt class="docutils literal"><span class="pre">r.reset([t])</span></tt> was
called) to time <em>t</em> (or to the current simulation time, <tt class="docutils literal"><span class="pre">now()</span></tt>,
if <em>t</em> is missing).</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">r.__str__(</span> <span class="pre">)</span></tt> is a string that briefly describes the current state
of the monitor. This can be used in a print statement.</p>
</li>
</ul>
</div>
<div class="section" id="special-methods-for-monitor">
<span id="index-84"></span><h3>Special methods for Monitor<a class="headerlink" href="#special-methods-for-monitor" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">Monitor</span></tt> variety of Recorder is a sub-class of <tt class="docutils literal"><span class="pre">List</span></tt>
and has a few extra methods:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">m[i]</span></tt> holds the observation <tt class="docutils literal"><span class="pre">i</span></tt> as a two-item list, <em>[ti, yi]</em></li>
<li><tt class="docutils literal"><span class="pre">m.yseries(</span> <span class="pre">)</span></tt> is a list of the recorded data values, <em>yi</em></li>
<li><tt class="docutils literal"><span class="pre">m.tseries(</span> <span class="pre">)</span></tt> is a list of the recorded times, <em>ti</em></li>
</ul>
</div>
<div class="section" id="histograms">
<span id="index-85"></span><h3>Histograms<a class="headerlink" href="#histograms" title="Permalink to this headline">¶</a></h3>
<p>A <tt class="docutils literal"><span class="pre">Histogram</span></tt> is a derived class of <tt class="docutils literal"><span class="pre">list</span></tt> that counts the
observations that fall into a number of specified ranges, called bins.
A histogram object can be displayed either by printing it out in text
form using <tt class="docutils literal"><span class="pre">printHistogram</span></tt> method or using the <tt class="docutils literal"><span class="pre">plotHistogram</span></tt> method in the
<tt class="docutils literal"><span class="pre">SimPy.SimPlot</span></tt> package.</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">h</span> <span class="pre">=</span> <span class="pre">Histogram(low=&lt;float&gt;,high=&lt;float&gt;,nbins=&lt;integer&gt;)</span></tt> is a
histogram object that counts the
number of <tt class="docutils literal"><span class="pre">y</span></tt> values in each of its bins, based on the
recorded <tt class="docutils literal"><span class="pre">y</span></tt> values.</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">low</span></tt> is the nominal lowest value of the histogram (default=0.0)</li>
<li><tt class="docutils literal"><span class="pre">high</span></tt> is the nominal highest value of the histogram (default=100.0)</li>
<li><tt class="docutils literal"><span class="pre">nbins</span></tt> is the number of bins between <tt class="docutils literal"><span class="pre">low</span></tt> and <tt class="docutils literal"><span class="pre">high</span></tt> into which
the histogram is to be divided (default=10).
SimPy automatically constructs an additional two bins
to count the
number of <tt class="docutils literal"><span class="pre">y</span></tt> values <tt class="docutils literal"><span class="pre">under</span></tt> the <tt class="docutils literal"><span class="pre">low</span></tt> value and the number
<tt class="docutils literal"><span class="pre">over</span></tt> the <tt class="docutils literal"><span class="pre">high</span></tt> value. Thus, the total number of bins
actually used is <tt class="docutils literal"><span class="pre">nbins</span> <span class="pre">+</span> <span class="pre">2</span></tt>. The number of <tt class="docutils literal"><span class="pre">y</span></tt>
values in each of these bins is counted and assigned
to the appropriate bin.</li>
</ul>
<div class="figure align-center" style="width: 500">
<img alt="Histogram" src="../_images/Mon006.png" />
<p class="caption">A Histogram contains the number of observed <tt class="docutils literal"><span class="pre">y</span></tt> values falling
into each of its <tt class="docutils literal"><span class="pre">nbins+2</span></tt> bins.</p>
</div>
</li>
</ul>
<p>A Histogram, <em>h</em>, can be printed out in text form using</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">h.printHistogram(fmt=&quot;%s&quot;)</span></tt> prints out a histogram in a standard
format.<ul>
<li><tt class="docutils literal"><span class="pre">fmt</span></tt> is a python string format for the bin range values.</li>
</ul>
</li>
</ul>
<hr class="docutils" id="index-86" />
<p><strong>Example</strong> Printing a histogram from a Tally:</p>
<div class="highlight-python"><pre>from SimPy.Simulation import *
import random as r

print version

t=Tally(name="myTally",ylab="wait time (sec)")
t.setHistogram(low=0.0,high=1.0,nbins=10)
for i in range(100000):
    t.observe(y=r.random())
print t.printHistogram(fmt="%6.4f")</pre>
</div>
<p>This gives a printed histogram like this:</p>
<div class="highlight-python"><pre>Histogram for myTally:
Number of observations: 100000
          wait time (sec) &lt; 0.0000:      0 (cum:      0/  0.0%)
0.0000 &lt;= wait time (sec) &lt; 0.1000:   9983 (cum:   9983/ 10.0%)
0.1000 &lt;= wait time (sec) &lt; 0.2000:  10121 (cum:  20104/ 20.1%)
0.2000 &lt;= wait time (sec) &lt; 0.3000:   9800 (cum:  29904/ 29.9%)
0.3000 &lt;= wait time (sec) &lt; 0.4000:   9911 (cum:  39815/ 39.8%)
0.4000 &lt;= wait time (sec) &lt; 0.5000:   9996 (cum:  49811/ 49.8%)
0.5000 &lt;= wait time (sec) &lt; 0.6000:   9881 (cum:  59692/ 59.7%)
0.6000 &lt;= wait time (sec) &lt; 0.7000:  10144 (cum:  69836/ 69.8%)
0.7000 &lt;= wait time (sec) &lt; 0.8000:  10029 (cum:  79865/ 79.9%)
0.8000 &lt;= wait time (sec) &lt; 0.9000:  10088 (cum:  89953/ 90.0%)
0.9000 &lt;= wait time (sec) &lt; 1.0000:  10047 (cum: 100000/100.0%)
1.0000 &lt;= wait time (sec)         :      0 (cum: 100000/100.0%)</pre>
</div>
<hr class="docutils" />
<p>Although both Tallys and Monitors can return a histogram of the data, they
furnish histogram data in different ways.</p>
<ul class="simple">
<li>The Tally object accumulates the histogram&#8217;s bin counts as each
value is observed during the simulation run. Since none of the
individual values are preserved, the <tt class="docutils literal"><span class="pre">setHistogram</span></tt> method must be
called to provide a histogram object to hold the accumulated bin
counts before any values are actually observed.</li>
<li>The Monitor object stores all its data, so the accumulated bin
counts can be computed whenever they are desired. Thus, the
histogram need not be set up until it is needed and this can be done
after the data has been gathered.</li>
</ul>
<div class="section" id="setting-up-a-histogram-for-a-tally-object">
<span id="index-87"></span><h4>Setting up a Histogram for a Tally object<a class="headerlink" href="#setting-up-a-histogram-for-a-tally-object" title="Permalink to this headline">¶</a></h4>
<p>To establish a histogram for a Tally object, <tt class="docutils literal"><span class="pre">r</span></tt>, we call the
<tt class="docutils literal"><span class="pre">setHistogram</span></tt> method with appropriate arguments before we observe any
data, e.g.,</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">r.setHistogram(name</span> <span class="pre">=</span> <span class="pre">'',low=0.0,high=100.0,nbins=10)</span></tt></li>
</ul>
<p>As usual, <tt class="docutils literal"><span class="pre">name</span></tt> is a descriptive title for the histogram (defaults to blank).
Then, after <tt class="docutils literal"><span class="pre">observing</span></tt> the data:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">h=r.getHistogram(</span> <span class="pre">)</span></tt> returns a completed histogram using the
histogram parameters as set up.</li>
</ul>
<hr class="docutils" id="index-88" />
<p><strong>Example</strong> In the following example we establish a <tt class="docutils literal"><span class="pre">Tally</span></tt> recorder
to observe values of an exponential random variate. It uses a
histogram with 30 bins (plus the under- and over-count bins):</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">expovariate</span>

<span class="n">r</span> <span class="o">=</span> <span class="n">Tally</span><span class="p">(</span><span class="s">&#39;Tally&#39;</span><span class="p">)</span>                          <span class="c"># define a tally object, r</span>
<span class="n">r</span><span class="o">.</span><span class="n">setHistogram</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;exponential&#39;</span><span class="p">,</span>
               <span class="n">low</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span><span class="n">high</span><span class="o">=</span><span class="mf">20.0</span><span class="p">,</span><span class="n">nbins</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>  <span class="c"># set before observations</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">):</span>    <span class="c"># make the observations</span>
   <span class="n">y</span> <span class="o">=</span> <span class="n">expovariate</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
   <span class="n">r</span><span class="o">.</span><span class="n">observe</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="n">h</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">getHistogram</span><span class="p">()</span>     <span class="c"># return the completed histogram</span>
</pre></div>
</td></tr></table></div>
<hr class="docutils" />
</div>
<div class="section" id="setting-up-a-histogram-for-a-monitor-object">
<span id="index-89"></span><h4>Setting up a Histogram for a Monitor object<a class="headerlink" href="#setting-up-a-histogram-for-a-monitor-object" title="Permalink to this headline">¶</a></h4>
<p>For Monitor objects, a histogram can be set up and returned in
a single call, e.g.,</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">h</span> <span class="pre">=</span> <span class="pre">r.histogram(low=0.0,high=100.0,nbins=10)</span></tt></li>
</ul>
<p>This  call is  equivalent to the following pair:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">r.setHistogram(name</span> <span class="pre">=</span> <span class="pre">'',low=0.0,high=100.0,nbins=10)</span></tt></li>
<li><tt class="docutils literal"><span class="pre">h</span> <span class="pre">=</span> <span class="pre">r.getHistogram(</span> <span class="pre">)</span></tt>, which returns the completed histogram.</li>
</ul>
<hr class="docutils" id="index-90" />
<p><strong>Example</strong> Here we establish a <tt class="docutils literal"><span class="pre">Monitor</span></tt> to observe values of an
exponential random variate. It uses a histogram with 30 bins (plus the
under- and over-count bins):</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">SimPy.Simulation</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">expovariate</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">Monitor</span><span class="p">()</span>        <span class="c"># define the Monitor object, m</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">):</span>    <span class="c"># make the observations</span>
   <span class="n">y</span> <span class="o">=</span> <span class="n">expovariate</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
   <span class="n">m</span><span class="o">.</span><span class="n">observe</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

   <span class="c"># set up and return the completed histogram</span>
<span class="n">h</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">histogram</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">nbins</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
<hr class="docutils" />
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
</div>
<div class="section" id="other-links">
<h2><a class="toc-backref" href="#id24">Other Links</a><a class="headerlink" href="#other-links" title="Permalink to this headline">¶</a></h2>
<p>Several example SimPy models are included with the SimPy code
distribution in the file <tt class="docutils literal"><span class="pre">SimPyModels</span></tt>.</p>
<p>Klaus Muller and Tony Vignaux, <em>SimPy: Simulating Systems in Python</em>,
O&#8217;Reilly ONLamp.com, 2003-Feb-27,  <a class="reference external" href="http://www.onlamp.com/pub/a/python/2003/02/27/simpy.html">http://www.onlamp.com/pub/a/python/2003/02/27/simpy.html</a></p>
<p>Norman Matloff, <em>Introduction to the SimPy Discrete-Event Simulation
Package</em>, U Cal: Davis, 2003,
<a class="reference external" href="http://heather.cs.ucdavis.edu/~matloff/simpy.html">http://heather.cs.ucdavis.edu/~matloff/simpy.html</a></p>
<p>David Mertz, <em>Charming Python: SimPy simplifies complex models</em>, IBM
Developer Works, Dec 2002,
<a class="reference external" href="http://www-106.ibm.com/developerworks/linux/library/l-simpy.html">http://www-106.ibm.com/developerworks/linux/library/l-simpy.html</a></p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
<div class="section" id="acknowledgments">
<h2><a class="toc-backref" href="#id25">Acknowledgments</a><a class="headerlink" href="#acknowledgments" title="Permalink to this headline">¶</a></h2>
<p>We thank those users who have sent comments to correct or improve this
text. These include: F. Benichu, Bob Helmbold, M. Matti. We will be
grateful for further corrections or suggestions.</p>
</div>
<div class="section" id="appendices">
<h2><a class="toc-backref" href="#id26">Appendices</a><a class="headerlink" href="#appendices" title="Permalink to this headline">¶</a></h2>
<div class="section" id="a0-changes-from-the-previous-version-of-simpy">
<h3>A0. Changes from the previous version of SimPy<a class="headerlink" href="#a0-changes-from-the-previous-version-of-simpy" title="Permalink to this headline">¶</a></h3>
<p>SimPy 2.2b1 differs from version 2.1 in the following ways:</p>
<p><strong>Additions:</strong></p>
<p><strong>Changes:</strong></p>
<ul class="simple">
<li>The Unit tests have been rewritten</li>
<li>The directory structure of the release has been simplified</li>
<li>The documentation has had some minor changes</li>
</ul>
</div>
<div class="section" id="a1-simpy-error-messages">
<span id="index-91"></span><h3>A1. SimPy Error Messages<a class="headerlink" href="#a1-simpy-error-messages" title="Permalink to this headline">¶</a></h3>
<div class="section" id="advisory-messages">
<h4>Advisory messages<a class="headerlink" href="#advisory-messages" title="Permalink to this headline">¶</a></h4>
<p>These messages are returned by <tt class="docutils literal"><span class="pre">simulate(</span> <span class="pre">)</span></tt>, as in
<tt class="docutils literal"><span class="pre">message=simulate(until=123)</span></tt>.</p>
<p>Upon a normal end of a simulation, <tt class="docutils literal"><span class="pre">simulate(</span> <span class="pre">)</span></tt> returns the message:</p>
<ul class="simple">
<li><strong>SimPy: Normal exit</strong>. This means that no errors have occurred and
the simulation has run to the time specified by the <tt class="docutils literal"><span class="pre">until</span></tt> parameter.</li>
</ul>
<p>The following messages, returned by <tt class="docutils literal"><span class="pre">simulate(</span> <span class="pre">)</span></tt>, are produced at a premature
termination of the simulation but allow continuation of the program.</p>
<ul class="simple">
<li><strong>SimPy: No more events at time x</strong>. All processes were completed prior
to the <em>endtime</em> given in <em>simulate(until=endtime)</em>.</li>
<li><strong>SimPy: No activities scheduled</strong>. No activities were scheduled
when <em>simulate( )</em> was called.</li>
</ul>
</div>
<div class="section" id="fatal-error-messages">
<h4>Fatal error messages<a class="headerlink" href="#fatal-error-messages" title="Permalink to this headline">¶</a></h4>
<p>These messages are generated when SimPy-related fatal  exceptions occur.
They end the SimPy program. Fatal SimPy error messages are output to
<em>sysout</em>.</p>
<ul class="simple">
<li><strong>Fatal SimPy error: activating function which is not a generator (contains no &#8216;yield&#8217;)</strong>.
A process tried to (re)activate a function which is not a
SimPy process (=Python generator). SimPy processes must contain
at least one <em>yield . . .</em> statement.</li>
<li><strong>Fatal SimPy error: Simulation not initialized</strong>. The SimPy program
called <em>simulate( )</em> before calling <em>initialize( )</em>.</li>
<li><strong>SimPy: Attempt to schedule event in the past</strong>: A <em>yield hold</em> statement
has a negative delay time parameter.</li>
<li><strong>SimPy: initialBuffered exceeds capacity</strong>: Attempt to initialize a Store
or Level with more units in the buffer than its capacity allows.</li>
<li><strong>SimPy: initialBuffered param of Level negative: x</strong>: Attempt to
initialize a Level with a negative amount x in the buffer.</li>
<li><strong>SimPy: Level: wrong type of initialBuffered (parameter=x)</strong>: Attempt to
initialize a buffer with a non-numerical initial buffer content x.</li>
<li><strong>SimPy: Level: put parameter not a number</strong>: Attempt to add a
non-numerical amount to a Level&#8217;s buffer.</li>
<li><strong>SimPy: Level: put parameter not positive number</strong>: Attempt to add
a negative number to a Level&#8217;s amount.</li>
<li><strong>SimPy: Level: get parameter not positive number: x</strong>: Attempt to
get a negative amount x from a Level.</li>
<li><strong>SimPy: Store: initialBuffered not a list</strong>: Attempt to initialize
a Store with other than a list of items in the buffer.</li>
<li><strong>SimPy: Item to put missing in yield put stmt</strong>: A <em>yield put</em> was
malformed by not having a parameter for the item(s) to put into the
Store.</li>
<li><strong>SimPy: put parameter is not a list</strong>: <em>yield put</em> for a Store must
have a parameter which is a list of items to put into the buffer.</li>
<li><strong>SimPy: Store: get parameter not positive number: x</strong>: A <em>yield
get</em> for a Store had a negative value for the number to get from the
buffer.</li>
<li><strong>SimPy: Fatal error: illegal command: yield x</strong>: A <em>yield</em>
statement with an undefined command code (first parameter) x was
executed.</li>
</ul>
</div>
<div class="section" id="monitor-error-messages">
<h4>Monitor error messages<a class="headerlink" href="#monitor-error-messages" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li><strong>SimPy: No observations for mean</strong>. No observations were made by the
monitor before attempting to calculate the mean.</li>
<li><strong>SimPy: No observations for sample variance</strong>. No observations were made by the
monitor before attempting to calculate the sample variance.</li>
<li><strong>SimPy: No observations for timeAverage</strong>, No observations
were made by the monitor before attempting to calculate the time-average.</li>
<li><strong>SimPy: No elapsed time for timeAverage</strong>. No simulation
time has elapsed before attempting to calculate the time-average.</li>
</ul>
</div>
</div>
<div class="section" id="a2-simpy-process-states">
<span id="index-92"></span><h3>A2. SimPy Process States<a class="headerlink" href="#a2-simpy-process-states" title="Permalink to this headline">¶</a></h3>
<p>From the viewpoint of the model builder a SimPy process, <em>p</em>,
can at any time be in one of the following states:</p>
<ul class="simple">
<li><strong>Active</strong>: Waiting for a scheduled event. This state simulates an
activity in the model.  Simulated time passes in this
state. The process state <em>p.active( )</em> returns <em>True</em>.</li>
<li><strong>Passive</strong>: Not active or terminated. Awaiting <em>(re-)activation</em> by
another process.  This state simulates a real world process which
has not finished and is waiting for some trigger to continue. Does
not change simulation time.  <em>p.passive( )</em> returns <em>True</em>.</li>
<li><strong>Terminated</strong>: The process has executed all its action statements.
If referenced, it serves as a data instance. <em>p.terminated( )</em>
returns <em>True</em></li>
</ul>
<p>Initially (upon creation of the Process instance), a process returns <em>passive</em>.</p>
<p>In addition, a SimPy process, <em>p</em>,  can be in the following (sub)states:</p>
<ul class="simple">
<li><strong>Interrupted</strong>: Active process has been interrupted by another
process. It can immediately respond to the interrupt. This
simulates an interruption of a simulated activity before its
scheduled completion time.  <em>p.interrupted( )</em> returns <em>True</em>.</li>
<li><strong>Queuing</strong>: Active process has requested a busy resource and is
waiting (passive) to be reactivated upon resource
availability. <em>p.queuing(a_resource)</em> returns <em>True</em>.</li>
</ul>
</div>
<div class="section" id="a3-simplot-the-simpy-plotting-utility">
<h3>A3. SimPlot, The SimPy plotting utility<a class="headerlink" href="#a3-simplot-the-simpy-plotting-utility" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">SimPlot</span></tt> provides an easy way to graph the results of simulation runs.</p>
</div>
<div class="section" id="a4-simgui-the-simpy-graphical-user-interface">
<h3>A4. SimGUI, The SimPy Graphical User Interface<a class="headerlink" href="#a4-simgui-the-simpy-graphical-user-interface" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">SimGUI</span></tt>  provides a  way for users to interact with a SimPy program,
changing its parameters and examining the output.</p>
</div>
<div class="section" id="a5-simulationtrace-the-simpy-tracing-utility">
<h3>A5. SimulationTrace, the SimPy tracing utility<a class="headerlink" href="#a5-simulationtrace-the-simpy-tracing-utility" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">SimulationTrace</span></tt> has been developed to give users insight into the
dynamics of the execution of SimPy simulation programs. It can help
developers with testing and users with explaining SimPy models to themselves
and others (e.g., for documentation or teaching purposes).</p>
</div>
<div class="section" id="a6-simulationstep-the-simpy-event-stepping-utility">
<h3>A6. SimulationStep, the SimPy event stepping utility<a class="headerlink" href="#a6-simulationstep-the-simpy-event-stepping-utility" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">SimulationStep</span></tt> can assist with debugging models, interacting with them on
an event-by-event basis, getting event-by-event output from a model (e.g.
for plotting purposes), etc.</p>
<p>It caters for:</p>
<blockquote>
<div><ul class="simple">
<li>running a simulation model, while calling a user-defined procedure after every event,</li>
<li>running a simulation model one event at a time by repeated calls,</li>
<li>starting and stopping the event-stepping mode under program control.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="a7-simulationrt-a-real-time-synchronizing-utility">
<h3>A7. SimulationRT, a real-time synchronizing utility<a class="headerlink" href="#a7-simulationrt-a-real-time-synchronizing-utility" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">SimulationRT</span></tt> allows synchronizing simulation time and real (wall-clock) time.
This capability can be used to implement, e.g., interactive game applications or
to demonstrate a model&#8217;s execution in real time.</p>
<p>[Return to <a class="reference internal" href="#contents">Top</a> ]</p>
</div>
</div>
<div class="section" id="glossary">
<span id="index-93"></span><span id="id12"></span><h2><a class="toc-backref" href="#id27">Glossary</a><a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
<p>(Note: Terms in <em>italics</em> refer to other special terms.
Items in <tt class="docutils literal"><span class="pre">code</span> <span class="pre">font</span></tt> are code fragments or specific code names.):</p>
<dl class="docutils">
<dt>activeQ</dt>
<dd>A <em>Resource</em> object automatically creates and maintains its
own activeQ, the queue (list) of process objects that are
currently using one of the Resource&#8217;s units. See
<a class="reference internal" href="#resources">Resources</a>. (See also the Glossary entry for <em>waitQ</em>.)</dd>
<dt>activate</dt>
<dd>Commands a <em>process object</em> to being executing its <em>PEM</em>.
See <a class="reference internal" href="#starting-and-stopping-simpy-process-objects">Starting and stopping SimPy process objects</a>.</dd>
<dt>Backus-Naur Form (BNF) notation</dt>
<dd><p class="first">This manual occasionally uses a modified Backus-Naur Form notation to
exhibit command syntax,
as in the description of the <em>activate</em> command:</p>
<div class="highlight-python"><pre>activate(p, p.PEM([args]) [,{at=t|delay=period}] [,prior=False])</pre>
</div>
<p class="last">In this notation, square brackets [ ] indicate items that are optional,
braces { } indicate items of which zero or more may be present,
and a vertical bar | indicates a choice between alternatives
(with none of them being a possibility).</p>
</dd>
<dt>cancel</dt>
<dd>Deletes all of a <em>process object&#8217;s</em> scheduled future events.
See <a class="reference internal" href="#starting-and-stopping-simpy-process-objects">Starting and stopping SimPy process objects</a>.</dd>
<dt>entity</dt>
<dd>An alternative name for <em>process object</em>.</dd>
<dt>event</dt>
<dd>A SimEvent object. See <a class="reference internal" href="#advanced-synchronization-scheduling-capabilities">Advanced synchronization/scheduling capabilities</a>.</dd>
<dt>FIFO</dt>
<dd>An attribute of a resource object (i.e., a <a class="reference internal" href="#resources">Resource</a>, <a class="reference internal" href="#levels">Level</a>,
or <a class="reference internal" href="#stores">Store</a>)
indicating that an associated queue (e.g., the <em>ActiveQ</em>, <em>waitQ</em>,
<em>getQ</em>, or <em>putQ</em>) is to be
kept in FIFO order.
(See also the Glossary entries for <em>PriorityQ</em> and <em>qType</em>.)</dd>
<dt>getQ</dt>
<dd>The queue of processes waiting to take something from a
<a class="reference internal" href="#levels">Level</a> or <a class="reference internal" href="#stores">Store</a> resource. See also the Glossary entry for <em>putQ</em>.</dd>
<dt>interrupt</dt>
<dd>Requests a &#8220;victim&#8221; <em>process object</em> to interrupt (i.e., to immediately and
prematurely end) its current
<tt class="docutils literal"><span class="pre">yield</span> <span class="pre">hold,...</span></tt> command. (Note: A process object cannot interrupt itself.)
See <a class="reference internal" href="#asynchronous-interruptions">Asynchronous interruptions</a>.</dd>
<dt>Level</dt>
<dd>A particular type of <em>resource facility</em> that models the
production and consumption of a
homogeneous undifferentiated &#8220;material.&#8221; <em>Process objects</em> can increase or
decrease the amount of material in a Level resource facility. See <a class="reference internal" href="#levels">Levels</a>.</dd>
<dt>Monitor</dt>
<dd>A data recorder that compiles basic statistics as a function of time on
variables such
as waiting times and queue lengths. (Note: Monitors can also preserve complete
time-series
data for post-simulation analyses.) See <a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a>.</dd>
<dt>monitorType</dt>
<dd>The type of <a class="reference internal" href="#recording-simulation-results">Recorder</a> to be used for recording simulation results.
Usually this is either a <a class="reference internal" href="#defining-tallys-and-monitors">Monitor</a> or a <a class="reference internal" href="#defining-tallys-and-monitors">Tally</a>. (See also the
Glossary entry for <em>Recorder</em>.)</dd>
<dt>monitored</dt>
<dd>A (boolean) attribute of a <em>resource</em> object indicating whether to
keep a record
of its activity. See <a class="reference internal" href="#recording-simulation-results">Recorder</a>.</dd>
<dt>passivate</dt>
<dd>Halts (&#8220;freezes&#8221;) a <em>process object&#8217;s</em> PEM. The process object  becomes
&#8220;passive&#8221;.
See <a class="reference internal" href="#starting-and-stopping-simpy-process-objects">Starting and stopping SimPy Process Objects</a>.</dd>
<dt>PEM</dt>
<dd>An abbreviation for <em>Process Execution Method</em>, q.v.</dd>
<dt>preempt</dt>
<dd>To force a <em>process</em> object currently using a <em>resource</em> unit
to release it and make it
available for use by another process object. See <a class="reference internal" href="#preemptive-requests-for-a-resource-unit">Preemptive requests for
a Resource unit</a>.</dd>
<dt>preemptable</dt>
<dd>A settable attribute of <em>Resource</em> objects. The Resource object&#8217;s
units are preemptable if
<tt class="docutils literal"><span class="pre">preemptable==True</span></tt>, otherwise not. See <a class="reference internal" href="#preemptive-requests-for-a-resource-unit">Preemptive requests for a Resource
unit</a>.</dd>
<dt>priority</dt>
<dd>A non-negative integer or real value controlling the order of <em>process</em> objects
in a queue. Higher values represent higher priority. Higher priority process
objects are placed ahead of lower priority ones in the queue. See also
the Glossary entry for <em>FIFO</em>.</dd>
<dt>PriorityQ</dt>
<dd>An attribute of a resource object (i.e., a <a class="reference internal" href="#resources">Resource</a>, <a class="reference internal" href="#levels">Level</a>,
or <a class="reference internal" href="#stores">Store</a>)
indicating that an associated queue (e.g., the <em>ActiveQ</em>, <em>waitQ</em>,
<em>getQ</em>, or <em>putQ</em>) is to be
kept in order of <em>priority</em>.
(See also the Glossary entries for <em>FIFO</em>, <em>qType</em>.)</dd>
<dt>process</dt>
<dd>We usually call both process objects and their classes &#8220;processes&#8221;
(with a small &#8220;p&#8221;).
Thus, &#8220;process&#8221; may refer to a <em>Process class</em> or to
a <em>process object</em>, depending on context. To avoid ambiguity
or for added emphasis
we often explicitly state whether a class or an object is intended.</dd>
<dt>Process class</dt>
<dd>A class that inherits from SimPy&#8217;s <tt class="docutils literal"><span class="pre">Process</span></tt> class and contains
at least one <em>Process Execution Method</em>.
Process classes may also contain other methods &#8211;
in particular they may contain an <tt class="docutils literal"><span class="pre">__init__</span></tt> method.
See <a class="reference internal" href="#processes">Processes</a>.</dd>
<dt>Process Execution Method</dt>
<dd>A <em>Process class</em> method that contains at least one <tt class="docutils literal"><span class="pre">yield</span> <span class="pre">...</span></tt>
statement. See <a class="reference internal" href="#defining-a-process">Defining a process</a>.</dd>
<dt>process object</dt>
<dd>An object created from (i.e., an instance of) a <em>Process class</em>.
See <a class="reference internal" href="#processes">Processes</a>.</dd>
<dt>putQ</dt>
<dd>The queue of processes waiting to add something to a <a class="reference internal" href="#levels">Level</a>
or <a class="reference internal" href="#stores">Store</a> resource. See also the Glossary entry for <em>getQ</em>.</dd>
<dt>reactivate</dt>
<dd>Reactivates (&#8220;unfreezes&#8221;) a passivated or a terminated <em>process object&#8217;s</em> PEM.
The <em>process object</em> becomes &#8220;active&#8221;.  See <a class="reference internal" href="#starting-and-stopping-simpy-process-objects">Starting and stopping SimPy Process
Objects</a>.</dd>
<dt>Recorder</dt>
<dd>A device for recording simulation results. Unless otherwise specified, it
usually refers either to a <a class="reference internal" href="#defining-tallys-and-monitors">Monitor</a> or a <a class="reference internal" href="#defining-tallys-and-monitors">Tally</a>. However,
Recorders also include histograms and observers.
See <a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a>
for <em>Monitors</em>, <em>Tallys</em>, and the other devices for
recording simulation results.</dd>
<dt>renege</dt>
<dd>To leave a queue before acquiring a resource unit. See <a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">Reneging &#8211; leaving
a queue before acquiring a resource</a>.</dd>
<dt>resource</dt>
<dd>Same as &#8220;resource facility.&#8221;
A congestion point at which <em>process objects</em> may need to queue for access to
resources. The term &#8220;resource&#8221; (with a small &#8220;r&#8221;) is used as a generic term
for the individual resource facilities provided by SimPy (i.e.,
<a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a>, and <a class="reference internal" href="#stores">Stores</a>).</dd>
<dt>qType</dt>
<dd>An attribute of <em>resource</em> objects indicating whether an
associated queue is to be
kept in <em>FIFO</em> or <em>PriorityQ</em> order. See the Glossary entries
for <em>waitQ</em>, <em>ActiveQ</em>, <em>putQ</em>,and <em>getQ</em>.
See also the treatment of these queues in the sections on
the individual resources (i.e., <a class="reference internal" href="#resources">Resources</a>, <a class="reference internal" href="#levels">Levels</a>, and <a class="reference internal" href="#stores">Stores</a>).</dd>
<dt>Resource</dt>
<dd>A particular type of <em>resource facility</em> that possesses several
identical <em>resource units</em>.
A <em>process object</em> may acquire one (and only one) of the Resource&#8217;s
resource units. See <a class="reference internal" href="#resources">Resources</a> .</dd>
<dt>Resource unit</dt>
<dd>One of the individual resources associated with a <em>Resource</em> type of
<em>resource facility</em>. See <a class="reference internal" href="#resources">Resources</a>.</dd>
<dt>SimEvent</dt>
<dd>The SimPy class for defining and creating SimEvent objects.
Occasionally designates a SimEvent
object when context makes that usage clear.
See <a class="reference internal" href="#advanced-synchronization-scheduling-capabilities">Advanced synchronization/scheduling capabilities</a>.</dd>
<dt>Store</dt>
<dd>A particular type of <em>resource facility</em> that models the
production and consumption of individual
items. <em>Process objects</em> can insert or remove items from the Store&#8217;s list
of available items. See <a class="reference internal" href="#stores">Stores</a>.</dd>
<dt>Tally</dt>
<dd>A particular type of <em>Recorder</em> that compiles basic statistics as a
function of time on variables such
as waiting times and queue lengths. (Note: Tallys do not preserve complete
time-series data for post-simulation analyses.) See
<a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a>.
(See also the Glossary entry for <em>monitorType</em>.)</dd>
<dt>unit (of a Resource)</dt>
<dd>One of the individual resource capabilities provided by a <em>Resource</em>. See
<a class="reference internal" href="#resources">Resources</a>.</dd>
<dt>waitQ</dt>
<dd>A <em>Resource</em> object automatically creates and maintains its own <em>waitQ</em>,
the queue (list) of process objects that have requested but not yet
received one of the Resource&#8217;s units. See <a class="reference internal" href="#resources">Resources</a>.
(See also the Glossary entry for <em>activeQ</em>.)</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sm_SimPy_Logo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">SimPy Manual</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#simulation-with-simpy">Simulation with SimPy</a><ul>
<li><a class="reference internal" href="#the-object-oriented-interface">The object-oriented interface</a></li>
<li><a class="reference internal" href="#alternative-simpy-simulation-libraries">Alternative SimPy simulation libraries</a></li>
</ul>
</li>
<li><a class="reference internal" href="#processes">Processes</a><ul>
<li><a class="reference internal" href="#defining-a-process">Defining a process</a></li>
<li><a class="reference internal" href="#creating-a-process-object">Creating a process object</a></li>
<li><a class="reference internal" href="#elapsing-time-in-a-process">Elapsing time in a Process</a><ul>
<li><a class="reference internal" href="#yield-hold">yield hold</a></li>
</ul>
</li>
<li><a class="reference internal" href="#starting-and-stopping-simpy-process-objects">Starting and stopping SimPy Process Objects</a><ul>
<li><a class="reference internal" href="#activate">activate</a></li>
<li><a class="reference internal" href="#start">start</a></li>
<li><a class="reference internal" href="#passivate">passivate</a></li>
<li><a class="reference internal" href="#reactivate">reactivate</a></li>
<li><a class="reference internal" href="#cancel">cancel</a></li>
<li><a class="reference internal" href="#a-source-fragment">A source fragment</a></li>
</ul>
</li>
<li><a class="reference internal" href="#asynchronous-interruptions">Asynchronous interruptions</a><ul>
<li><a class="reference internal" href="#interrupt">interrupt</a></li>
<li><a class="reference internal" href="#interrupted">interrupted</a></li>
<li><a class="reference internal" href="#interruptcause">interruptCause</a></li>
<li><a class="reference internal" href="#interruptleft">interruptLeft</a></li>
<li><a class="reference internal" href="#interruptreset">interruptReset</a></li>
</ul>
</li>
<li><a class="reference internal" href="#advanced-synchronization-scheduling-capabilities">Advanced synchronization/scheduling capabilities</a><ul>
<li><a class="reference internal" href="#creating-and-signalling-simevents">Creating and Signalling SimEvents</a><ul>
<li><a class="reference internal" href="#waiting-or-queueing-for-simevents">Waiting or Queueing for SimEvents</a></li>
</ul>
</li>
<li><a class="reference internal" href="#yield-waitevent">yield waitevent</a></li>
<li><a class="reference internal" href="#yield-queueevent">yield queueevent</a><ul>
<li><a class="reference internal" href="#finding-which-processes-are-waiting-queueing-for-an-event-and-which-events-fired">Finding Which Processes Are Waiting/Queueing for an Event, and Which Events Fired</a></li>
</ul>
</li>
<li><a class="reference internal" href="#waituntil-synchronization-waiting-for-any-condition">&#8220;waituntil&#8221; synchronization &#8211; waiting for any condition</a></li>
<li><a class="reference internal" href="#yield-waituntil">yield waituntil</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#resources">Resources</a><ul>
<li><a class="reference internal" href="#defining-a-resource-object">Defining a Resource object</a></li>
<li><a class="reference internal" href="#requesting-and-releasing-a-unit-of-a-resource">Requesting and releasing a unit of a Resource</a><ul>
<li><a class="reference internal" href="#yield-request">yield request</a></li>
<li><a class="reference internal" href="#yield-release">yield release</a></li>
</ul>
</li>
<li><a class="reference internal" href="#queue-order">Queue Order</a><ul>
<li><a class="reference internal" href="#non-priority-queueing">Non-priority queueing</a></li>
<li><a class="reference internal" href="#priority-requests-for-a-resource-unit">Priority requests for a Resource unit</a></li>
<li><a class="reference internal" href="#preemptive-requests-for-a-resource-unit">Preemptive requests for a Resource unit</a></li>
<li><a class="reference internal" href="#note-on-preemptive-requests-with-waitq-in-fifo-order">Note on preemptive requests with waitQ in FIFO order</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reneging-leaving-a-queue-before-acquiring-a-resource">Reneging &#8211; leaving a queue before acquiring a resource</a><ul>
<li><a class="reference internal" href="#reneging-yield-request">Reneging yield request</a></li>
<li><a class="reference internal" href="#reneging-after-a-time-limit">Reneging after a time limit</a></li>
<li><a class="reference internal" href="#reneging-when-an-event-has-happened">Reneging when an event has happened</a></li>
<li><a class="reference internal" href="#exiting-conventions-and-preemptive-queues">Exiting conventions and preemptive queues</a></li>
</ul>
</li>
<li><a class="reference internal" href="#recording-resource-queue-lengths">Recording Resource queue lengths</a></li>
</ul>
</li>
<li><a class="reference internal" href="#levels">Levels</a><ul>
<li><a class="reference internal" href="#defining-a-level">Defining a Level</a></li>
<li><a class="reference internal" href="#getting-amounts-from-a-level">Getting amounts from a Level</a></li>
<li><a class="reference internal" href="#putting-amounts-into-a-level">Putting amounts into  a Level</a></li>
<li><a class="reference internal" href="#reneging">Reneging</a></li>
</ul>
</li>
<li><a class="reference internal" href="#stores">Stores</a><ul>
<li><a class="reference internal" href="#defining-a-store">Defining a Store</a></li>
<li><a class="reference internal" href="#putting-objects-into-a-store">Putting objects into a Store</a></li>
<li><a class="reference internal" href="#getting-objects-from-a-store">Getting objects from  a Store</a></li>
<li><a class="reference internal" href="#using-the-get-filter-function">Using the get filter function</a></li>
<li><a class="reference internal" href="#index-71">Reneging</a></li>
<li><a class="reference internal" href="#storing-objects-in-an-order">Storing objects in an order</a></li>
<li><a class="reference internal" href="#master-slave-modelling-with-a-store">Master/Slave modelling with a Store</a></li>
</ul>
</li>
<li><a class="reference internal" href="#random-number-generation">Random Number Generation</a></li>
<li><a class="reference internal" href="#recording-simulation-results">Recording Simulation Results</a><ul>
<li><a class="reference internal" href="#defining-tallys-and-monitors">Defining Tallys and Monitors</a></li>
<li><a class="reference internal" href="#observing-data">Observing data</a></li>
<li><a class="reference internal" href="#data-summaries">Data summaries</a></li>
<li><a class="reference internal" href="#special-methods-for-monitor">Special methods for Monitor</a></li>
<li><a class="reference internal" href="#histograms">Histograms</a><ul>
<li><a class="reference internal" href="#setting-up-a-histogram-for-a-tally-object">Setting up a Histogram for a Tally object</a></li>
<li><a class="reference internal" href="#setting-up-a-histogram-for-a-monitor-object">Setting up a Histogram for a Monitor object</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#other-links">Other Links</a></li>
<li><a class="reference internal" href="#acknowledgments">Acknowledgments</a></li>
<li><a class="reference internal" href="#appendices">Appendices</a><ul>
<li><a class="reference internal" href="#a0-changes-from-the-previous-version-of-simpy">A0. Changes from the previous version of SimPy</a></li>
<li><a class="reference internal" href="#a1-simpy-error-messages">A1. SimPy Error Messages</a><ul>
<li><a class="reference internal" href="#advisory-messages">Advisory messages</a></li>
<li><a class="reference internal" href="#fatal-error-messages">Fatal error messages</a></li>
<li><a class="reference internal" href="#monitor-error-messages">Monitor error messages</a></li>
</ul>
</li>
<li><a class="reference internal" href="#a2-simpy-process-states">A2. SimPy Process States</a></li>
<li><a class="reference internal" href="#a3-simplot-the-simpy-plotting-utility">A3. SimPlot, The SimPy plotting utility</a></li>
<li><a class="reference internal" href="#a4-simgui-the-simpy-graphical-user-interface">A4. SimGUI, The SimPy Graphical User Interface</a></li>
<li><a class="reference internal" href="#a5-simulationtrace-the-simpy-tracing-utility">A5. SimulationTrace, the SimPy tracing utility</a></li>
<li><a class="reference internal" href="#a6-simulationstep-the-simpy-event-stepping-utility">A6. SimulationStep, the SimPy event stepping utility</a></li>
<li><a class="reference internal" href="#a7-simulationrt-a-real-time-synchronizing-utility">A7. SimulationRT, a real-time synchronizing utility</a></li>
</ul>
</li>
<li><a class="reference internal" href="#glossary">Glossary</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../Manuals.html"
                        title="previous chapter">Manuals</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="SimPyOO_API.html"
                        title="next chapter">SimPy&#8217;s Object Oriented API</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/Manuals/Manual.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="SimPyOO_API.html" title="SimPy’s Object Oriented API"
             >next</a> |</li>
        <li class="right" >
          <a href="../Manuals.html" title="Manuals"
             >previous</a> |</li>
        <li><a href="../index.html">SimPy 2.3.1 documentation</a> &raquo;</li>
          <li><a href="../Manuals.html" >Manuals</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2002-2011, Klaus Müller, Tony Vignaux, Ontje Lünsdorf, Stefan Scherfke.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>