asimpy 0.4.1__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. {asimpy-0.4.1 → asimpy-0.5.1}/.gitignore +1 -0
  2. asimpy-0.5.1/PKG-INFO +484 -0
  3. asimpy-0.5.1/README.md +470 -0
  4. {asimpy-0.4.1 → asimpy-0.5.1}/docs/examples/index.html +5 -5
  5. asimpy-0.5.1/docs/index.html +1812 -0
  6. asimpy-0.5.1/docs/objects.inv +0 -0
  7. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/allof/index.html +1 -1
  8. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/barrier/index.html +1 -1
  9. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/environment/index.html +133 -0
  10. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/event/index.html +72 -28
  11. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/firstof/index.html +4 -2
  12. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/interrupt/index.html +52 -1
  13. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/process/index.html +35 -25
  14. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/resource/index.html +6 -10
  15. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/simqueue/index.html +32 -144
  16. {asimpy-0.4.1 → asimpy-0.5.1}/docs/reference/timeout/index.html +16 -4
  17. {asimpy-0.4.1 → asimpy-0.5.1}/docs/sitemap.xml.gz +0 -0
  18. {asimpy-0.4.1 → asimpy-0.5.1}/examples/firstof_queue.py +2 -2
  19. {asimpy-0.4.1 → asimpy-0.5.1}/examples/interrupt.py +1 -1
  20. {asimpy-0.4.1 → asimpy-0.5.1}/pyproject.toml +23 -30
  21. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/allof.py +1 -1
  22. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/environment.py +10 -7
  23. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/event.py +6 -2
  24. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/firstof.py +2 -1
  25. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/interrupt.py +1 -0
  26. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/process.py +7 -4
  27. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/resource.py +4 -6
  28. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/simqueue.py +16 -16
  29. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/timeout.py +8 -2
  30. asimpy-0.5.1/tests/test_allof.py +63 -0
  31. asimpy-0.5.1/tests/test_barrier.py +74 -0
  32. asimpy-0.5.1/tests/test_complex.py +64 -0
  33. asimpy-0.5.1/tests/test_environment.py +87 -0
  34. asimpy-0.5.1/tests/test_event.py +110 -0
  35. asimpy-0.5.1/tests/test_firstof.py +56 -0
  36. asimpy-0.5.1/tests/test_interrupt.py +21 -0
  37. asimpy-0.5.1/tests/test_priqueue.py +56 -0
  38. asimpy-0.5.1/tests/test_process.py +166 -0
  39. asimpy-0.5.1/tests/test_queue.py +129 -0
  40. asimpy-0.5.1/tests/test_resource.py +117 -0
  41. asimpy-0.5.1/tests/test_timeout.py +58 -0
  42. {asimpy-0.4.1 → asimpy-0.5.1}/uv.lock +229 -117
  43. asimpy-0.4.1/PKG-INFO +0 -241
  44. asimpy-0.4.1/README.md +0 -216
  45. asimpy-0.4.1/docs/index.html +0 -1391
  46. asimpy-0.4.1/docs/objects.inv +0 -0
  47. {asimpy-0.4.1 → asimpy-0.5.1}/.readthedocs.yaml +0 -0
  48. {asimpy-0.4.1 → asimpy-0.5.1}/CODE_OF_CONDUCT.md +0 -0
  49. {asimpy-0.4.1 → asimpy-0.5.1}/CONTRIBUTING.md +0 -0
  50. {asimpy-0.4.1 → asimpy-0.5.1}/LICENSE.md +0 -0
  51. {asimpy-0.4.1 → asimpy-0.5.1}/docs/.nojekyll +0 -0
  52. {asimpy-0.4.1 → asimpy-0.5.1}/docs/404.html +0 -0
  53. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/_mkdocstrings.css +0 -0
  54. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/images/favicon.png +0 -0
  55. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/bundle.79ae519e.min.js +0 -0
  56. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/bundle.79ae519e.min.js.map +0 -0
  57. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ar.min.js +0 -0
  58. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.da.min.js +0 -0
  59. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.de.min.js +0 -0
  60. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.du.min.js +0 -0
  61. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.el.min.js +0 -0
  62. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.es.min.js +0 -0
  63. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.fi.min.js +0 -0
  64. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.fr.min.js +0 -0
  65. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.he.min.js +0 -0
  66. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.hi.min.js +0 -0
  67. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.hu.min.js +0 -0
  68. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.hy.min.js +0 -0
  69. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.it.min.js +0 -0
  70. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ja.min.js +0 -0
  71. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.jp.min.js +0 -0
  72. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.kn.min.js +0 -0
  73. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ko.min.js +0 -0
  74. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.multi.min.js +0 -0
  75. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.nl.min.js +0 -0
  76. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.no.min.js +0 -0
  77. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.pt.min.js +0 -0
  78. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ro.min.js +0 -0
  79. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ru.min.js +0 -0
  80. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.sa.min.js +0 -0
  81. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.stemmer.support.min.js +0 -0
  82. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.sv.min.js +0 -0
  83. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.ta.min.js +0 -0
  84. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.te.min.js +0 -0
  85. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.th.min.js +0 -0
  86. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.tr.min.js +0 -0
  87. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.vi.min.js +0 -0
  88. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/min/lunr.zh.min.js +0 -0
  89. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/tinyseg.js +0 -0
  90. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/lunr/wordcut.js +0 -0
  91. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/workers/search.2c215733.min.js +0 -0
  92. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/javascripts/workers/search.2c215733.min.js.map +0 -0
  93. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/stylesheets/main.484c7ddc.min.css +0 -0
  94. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/stylesheets/main.484c7ddc.min.css.map +0 -0
  95. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/stylesheets/palette.ab4e12ef.min.css +0 -0
  96. {asimpy-0.4.1 → asimpy-0.5.1}/docs/assets/stylesheets/palette.ab4e12ef.min.css.map +0 -0
  97. {asimpy-0.4.1 → asimpy-0.5.1}/docs/conduct/index.html +0 -0
  98. {asimpy-0.4.1 → asimpy-0.5.1}/docs/contributing/index.html +0 -0
  99. {asimpy-0.4.1 → asimpy-0.5.1}/docs/license/index.html +0 -0
  100. {asimpy-0.4.1 → asimpy-0.5.1}/docs/requirements.txt +0 -0
  101. {asimpy-0.4.1 → asimpy-0.5.1}/docs/sitemap.xml +0 -0
  102. {asimpy-0.4.1 → asimpy-0.5.1}/docs/tutorial/01_minimal/index.html +0 -0
  103. {asimpy-0.4.1 → asimpy-0.5.1}/docs/tutorial/02_queue/index.html +0 -0
  104. {asimpy-0.4.1 → asimpy-0.5.1}/docs/tutorial/03_interrupt/index.html +0 -0
  105. {asimpy-0.4.1 → asimpy-0.5.1}/docs/tutorial/04_combine/index.html +0 -0
  106. {asimpy-0.4.1 → asimpy-0.5.1}/docs-requirements.txt +0 -0
  107. {asimpy-0.4.1 → asimpy-0.5.1}/examples/allof.py +0 -0
  108. {asimpy-0.4.1 → asimpy-0.5.1}/examples/barrier.py +0 -0
  109. {asimpy-0.4.1 → asimpy-0.5.1}/examples/firstof_timeout.py +0 -0
  110. {asimpy-0.4.1 → asimpy-0.5.1}/examples/priqueue.py +0 -0
  111. {asimpy-0.4.1 → asimpy-0.5.1}/examples/queue.py +0 -0
  112. {asimpy-0.4.1 → asimpy-0.5.1}/examples/resource.py +0 -0
  113. {asimpy-0.4.1 → asimpy-0.5.1}/examples/timeout.py +0 -0
  114. {asimpy-0.4.1 → asimpy-0.5.1}/mkdocs.yml +0 -0
  115. {asimpy-0.4.1 → asimpy-0.5.1}/output/allof.txt +0 -0
  116. {asimpy-0.4.1 → asimpy-0.5.1}/output/barrier.txt +0 -0
  117. {asimpy-0.4.1 → asimpy-0.5.1}/output/firstof_queue.txt +0 -0
  118. {asimpy-0.4.1 → asimpy-0.5.1}/output/firstof_timeout.txt +0 -0
  119. {asimpy-0.4.1 → asimpy-0.5.1}/output/interrupt.txt +0 -0
  120. {asimpy-0.4.1 → asimpy-0.5.1}/output/priqueue.txt +1 -1
  121. {asimpy-0.4.1 → asimpy-0.5.1}/output/queue.txt +0 -0
  122. {asimpy-0.4.1 → asimpy-0.5.1}/output/resource.txt +1 -1
  123. {asimpy-0.4.1 → asimpy-0.5.1}/output/timeout.txt +0 -0
  124. {asimpy-0.4.1 → asimpy-0.5.1}/pages/.pages +0 -0
  125. {asimpy-0.4.1 → asimpy-0.5.1}/pages/conduct.md +0 -0
  126. {asimpy-0.4.1 → asimpy-0.5.1}/pages/contributing.md +0 -0
  127. {asimpy-0.4.1 → asimpy-0.5.1}/pages/examples.md +0 -0
  128. {asimpy-0.4.1 → asimpy-0.5.1}/pages/index.md +0 -0
  129. {asimpy-0.4.1 → asimpy-0.5.1}/pages/license.md +0 -0
  130. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/allof.md +0 -0
  131. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/barrier.md +0 -0
  132. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/environment.md +0 -0
  133. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/event.md +0 -0
  134. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/firstof.md +0 -0
  135. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/interrupt.md +0 -0
  136. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/process.md +0 -0
  137. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/resource.md +0 -0
  138. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/simqueue.md +0 -0
  139. {asimpy-0.4.1 → asimpy-0.5.1}/pages/reference/timeout.md +0 -0
  140. {asimpy-0.4.1 → asimpy-0.5.1}/pages/tutorial/01_minimal.md +0 -0
  141. {asimpy-0.4.1 → asimpy-0.5.1}/pages/tutorial/02_queue.md +0 -0
  142. {asimpy-0.4.1 → asimpy-0.5.1}/pages/tutorial/03_interrupt.md +0 -0
  143. {asimpy-0.4.1 → asimpy-0.5.1}/pages/tutorial/04_combine.md +0 -0
  144. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/__init__.py +0 -0
  145. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/_adapt.py +0 -0
  146. {asimpy-0.4.1 → asimpy-0.5.1}/src/asimpy/barrier.py +0 -0
  147. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/01_minimal/environment.py +0 -0
  148. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/01_minimal/event.py +0 -0
  149. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/01_minimal/example_timeout.py +0 -0
  150. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/01_minimal/process.py +0 -0
  151. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/01_minimal/timeout.py +0 -0
  152. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/environment.py +0 -0
  153. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/event.py +0 -0
  154. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/example_queue.py +0 -0
  155. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/process.py +0 -0
  156. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/simqueue.py +0 -0
  157. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/02_queue/timeout.py +0 -0
  158. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/environment.py +0 -0
  159. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/event.py +0 -0
  160. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/example_interrupt.py +0 -0
  161. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/interrupt.py +0 -0
  162. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/process.py +0 -0
  163. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/simqueue.py +0 -0
  164. {asimpy-0.4.1 → asimpy-0.5.1}/tutorial/03_interrupt/timeout.py +0 -0
@@ -21,3 +21,4 @@ coverage.json
21
21
 
22
22
  # MacOS
23
23
  .DS_store
24
+ .aider*
asimpy-0.5.1/PKG-INFO ADDED
@@ -0,0 +1,484 @@
1
+ Metadata-Version: 2.4
2
+ Name: asimpy
3
+ Version: 0.5.1
4
+ Summary: A simple discrete event simulator using async/await
5
+ Author-email: Greg Wilson <gvwilson@third-bit.com>
6
+ Maintainer-email: Greg Wilson <gvwilson@third-bit.com>
7
+ License-File: LICENSE.md
8
+ Keywords: discrete event simulation,open source
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.12
13
+ Description-Content-Type: text/markdown
14
+
15
+ # asimpy
16
+
17
+ A simple discrete event simulation framework in Python using `async`/`await`.
18
+
19
+ - [Documentation][asimpy]
20
+ - [Package][package]
21
+ - [Repository][repo]
22
+
23
+ *Thanks to the creators of [SimPy][simpy] for inspiration.*
24
+
25
+ ## Core Concepts
26
+
27
+ Discrete event simulation (DES) simulates systems in which events occur at discrete points in time.
28
+ The simulation maintains a virtual clock and executes events in chronological order.
29
+ Unlike real-time systems,
30
+ the simulation jumps directly from one event time to the next,
31
+ skipping empty intervals.
32
+ (Time steps are often referred to as "ticks".)
33
+
34
+ ## Async/Await
35
+
36
+ Python's `async`/`await` syntax enables cooperative multitasking without threads.
37
+ Functions defined as `async def` return coroutine objects when called.
38
+ These coroutines can be paused at `await` points and later resumed.
39
+ More specifically,
40
+ when a coroutine executes `value = await expr`, it:
41
+
42
+ 1. yields the awaited object `expr` to its caller;
43
+ 2. suspends execution at that point;
44
+ 3. resumes later when `send(value)` is called on it; an thend
45
+ 4. returns the value passed to `send()` as the result of the `await` expression
46
+ inside the resumed coroutine.
47
+
48
+ [asimpy][asimpy] uses this mechanism to pause and resume coroutines to simulate simultaneously execution.
49
+ This is similar to the `yield`-based mechanism used in [SimPy][simpy].
50
+
51
+ ## `Environment`: Process and Event Management
52
+
53
+ The `Environment` class maintains the simulation state:
54
+
55
+ - `_now` is the current simulated time.
56
+ - `_pending` is a priority queue of callbacks waiting to be run in order of increasing time
57
+ (so that the next one to run is at the front of the queue).
58
+
59
+ `Environment.schedule(time, callback)` adds a callback to the queue.
60
+ The `_Pending` dataclass used to store it includes a serial number
61
+ to ensure deterministic ordering when multiple events occur at the same time.
62
+
63
+ `Environment.run()` implements the main simulation loop:
64
+
65
+ 1. Extract the next pending event from the priority queue.
66
+ 2. If an `until` parameter is specified and the event time exceeds it, stop.
67
+ 3. Execute the callback.
68
+ 4. If the callback doesn't return `NO_TIME` and the event time is greater than the current simulated time,
69
+ advance the clock.
70
+
71
+ The `NO_TIME` sentinel prevents time from advancing mistakenly when events are canceled.
72
+ This is explained in detail later.
73
+
74
+ ## `Event`: the Synchronization Primitive
75
+
76
+ The `Event` class represents an action that will complete in the future.
77
+ It has four members:
78
+
79
+ - `_triggered` indicates whether the event has completed.
80
+ - `_cancelled` indicaets whether the event was cancelled.
81
+ - `_value` is the event's result value.
82
+ - `_waiters` is a list of processes waiting for this event to occur.
83
+
84
+ When `Event.succeed(value)` is called, it:
85
+
86
+ 1. sets `_triggered` to `True` to show that the event has completed;
87
+ 2. stores the value for later retrieval;
88
+ 3. calls `resume(value)` on all waiting processes; and
89
+ 3. clears the list of waiting processes.
90
+
91
+ The internal `Event._add_waiter(proc)` method handles three cases:
92
+
93
+ 1. If the event has already completed (i.e., if `_triggered` is `True`),
94
+ it immediately calls `proc.resume(value)`.
95
+ 2. If the event has been canceled,
96
+ it does nothing.
97
+ 2. Otherwise, it adds `proc` to the list of waiting processes.
98
+
99
+ Finally,
100
+ `Event` implements `__await__()`,
101
+ which Python calls automatically when it executes `await evt`.
102
+ `Event.__await__` yields `self` so that the awaiting process gets the event back.
103
+
104
+ ## `Process`: Active Entities
105
+
106
+ `Process` is the base class for simulation processes.
107
+ (Unlike [SimPy][simpy], [asimpy][asimpy] uses a class rather than bare coroutines.)
108
+ When a `Process` is constructed, it:
109
+
110
+ 1. store a reference to the simulation environment;
111
+ 2. calls `init()` for subclass-specific setup
112
+ (the default implementation of this method does nothing);
113
+ 3. create a coroutine by calling `run()`; and
114
+ 4. schedules immediate execution of `Process._loop()`.
115
+
116
+ The `_loop()` method drives coroutine execution:
117
+
118
+ 1. If an interrupt is pending, throw it into the coroutine via `throw()`.
119
+ 2. Otherwise, send the value into the coroutine via `send()`.
120
+ 3. Receive the yielded event.
121
+ 4. Register this process as a waiter on that event
122
+
123
+ When `StopIteration` is raised by the coroutine,
124
+ the process is marked as done.
125
+ If any other exception occurs,
126
+ the process is marked as done and the exception is re-raised.
127
+
128
+ **Note:** The word "process" can be confusing.
129
+ These are *not* operating system processes with their own memory and permissions.
130
+
131
+ ### A Note on Scheduling
132
+
133
+ When an event completes it calls `proc.resume(value)` to schedules another iteration of `_loop()`
134
+ with the provided value.
135
+ This continues the coroutine past its `await` point.
136
+
137
+ ### A Note on Interrupts
138
+
139
+ The interrupt mechanism sets `_interrupt` and schedules immediate execution of the process.
140
+ The next `_loop()` iteration throws the interrupt into the coroutine,
141
+ where it can be caught with `try`/`except`.
142
+ This is a bit clumsy,
143
+ but is the only way to inject exceptions into running coroutines.
144
+
145
+ **Note:**
146
+ A process can *only* be interrupted at an `await` point.
147
+ Exceptions *cannot* be raised from the outside at arbitrary points.
148
+
149
+ ## `Timeout`: Waiting Until
150
+
151
+ A `Timeout` object schedules a callback at a future time.
152
+ `Timeout._fire()` method returns `NO_TIME` if the timeout has ben canceled,
153
+ which prevents canceled timeouts from accidentally advancing the simulation time.
154
+ Otherwise,
155
+ `Timeout._fire()` calls `succeed()` to trigger the event.
156
+
157
+ ## `Queue` and `PriorityQueue`: Exchanging Data
158
+
159
+ `Queue` enables processes to exchange data.
160
+ It has two members:
161
+
162
+ - `_items` is a list of items being passed between processes.
163
+ - `_getters` is a list of processes waiting for items.
164
+
165
+ The invariant for `Queue` is that one or the other list must be empty,
166
+ i.e.,
167
+ if there are processes waiting then there aren't any items to take,
168
+ while if there are items waiting to be taken there aren't any waiting processes.
169
+
170
+ `Queue.put(item)` immediately calls `evt.succeed(item)` if a process is waiting
171
+ to pass that item to the waiting process
172
+ (which is stored in the event).
173
+ Otherwise,
174
+ the item is appended to `queue._items`.
175
+
176
+ `Queue.get()` is a bit more complicated.
177
+ If the queue has items,
178
+ `queue.get()` creates an event that immediately succeeds with the first item.
179
+ If the queue is empty,
180
+ the call creates an event and adds the caller to the list of processes waiting to get items.
181
+
182
+ The complication is that if there *is* an item to get,
183
+ `queue.get()` sets the `_on_cancel` callback of the event to handles cancellation
184
+ by returning the item taken to the front of the queue.
185
+
186
+ `PriorityQueue` uses `heapq` operations to maintain ordering,
187
+ which means items must be comparable (i.e., must implement `__lt__`).
188
+ `get()` pops the minimum element;
189
+ `put()` pushes onto the heap and potentially satisfies a waiting getter.
190
+
191
+ ## `Resource`: Capacity-Limited Sharing
192
+
193
+ The `Resource` class simulates a shared resource with limited capacity.
194
+ It has three members:
195
+
196
+ - `capacity` is the maximum number of concurrent users.
197
+ - `_count` is the current number of users.
198
+ - `_waiters` is a list of processes waiting for the resource to be available.
199
+
200
+ If the resource is below capacity when `res.acquire()` is called,
201
+ it calls increments the internal count and immediately succeeds.
202
+ Otherwise,
203
+ it adds the caller to the list of waiting processes.
204
+ Similarly,
205
+ `res.release()` decrements the count and then checks the list of waiting processes.
206
+ If there are any,
207
+ it calls `evt.succeed()` for the event representing the first waiting process.
208
+
209
+ `Resource.acquire` depends on internal methods
210
+ `Resource._acquire_available` and `Resource._acquire_unavailable`,
211
+ both of which set the `_on_cancel` callback of the event they create
212
+ to restore the counter to its original state
213
+ or remove the event marking a waiting process.
214
+
215
+ Finally,
216
+ the context manager protocol methods `__aenter__` and `__aexit__`
217
+ allows processes to use `async with res`
218
+ to acquire and release a resource in a block.
219
+
220
+ ## `Barrier`: Synchronizing Multiple Processes
221
+
222
+ A `Barrier` holds multiple processes until they are explicitly released,
223
+ i.e.,
224
+ it allows the simulation to synchronize multiple processes.
225
+
226
+ - `wait()` creates an event and adds it to the list of waiters.
227
+ - `release()` calls `succeed()` on all waiting events and clears the list.
228
+
229
+ ## AllOf: Waiting for Multiple Events
230
+
231
+ `AllOf` and `FirstOf` are the most complicated parts of [asimpy][asimpy],
232
+ and the reason that parts such as cancellation management exist.
233
+ `AllOf` succeeds when all provided events complete.
234
+ It:
235
+
236
+ 1. converts each input to an event (discussed later);
237
+ 2. registers an `_AllOfWatcher` on each of those events;
238
+ 3. accumulates results in `_results` dictionary; and
239
+ 4. succeeds when all results collected.
240
+
241
+ Each watcher calls `_child_done(key, value)` when its event completes.
242
+ This stores the result and checks if all events are done.
243
+
244
+ ### A Note on Interface
245
+
246
+ A process calls `AllOf` like this:
247
+
248
+ ```python
249
+ await AllOf(self._env, a=self.timeout(5), b=self.timeout(10))
250
+ ```
251
+
252
+ The eventual result is a dictionary in which
253
+ the name of the events are keys and the results of the events are values;
254
+ in this case,
255
+ the keys will be `"a"` and `"b"`.
256
+ This gives callers an easy way to keep track of events,
257
+ though it *doesn't* support waiting on all events in a list.
258
+
259
+ `AllOf`'s interface would be tidier
260
+ if it didn't require the simulation environment as its first argument.
261
+ However,
262
+ removing it made the implementation significantly more complicated.
263
+
264
+ ## FirstOf: Racing Multiple Events
265
+
266
+ `FirstOf` succeeds as soon as *any* of the provided events succeeds,
267
+ and then cancels all of the other events.
268
+ To do this, it:
269
+
270
+ 1. converts each input to an event;
271
+ 2. registers a `_FirstOfWatcher` on each;
272
+ 3. on first completion, cancels all other events; and
273
+ 4. succeeds with a `(key, value)` to identify the winning event.
274
+
275
+ `FirstOf`'s `_done` flag prevents multiple completions.
276
+ When `_child_done()` is called,
277
+ it checks this flag,
278
+ cancels other waiters,
279
+ and succeeds.
280
+
281
+ ## Control Flow Example
282
+
283
+ Consider a process that waits 5 ticks:
284
+
285
+ ```python
286
+ class Waiter(Process):
287
+ async def run(self):
288
+ await self.timeout(5)
289
+ print("done")
290
+ ```
291
+
292
+ When it executes:
293
+
294
+ 1. Construction calls `__init__()`,
295
+ which creates a coroutine by calling `run()`
296
+ and immediately schedules `_loop()`.
297
+ 1. The first `_loop()` calls `send(None)` to the coroutine,
298
+ which executes to the `await`
299
+ and yields a `Timeout` event.
300
+ 1. `_loop()` registers this process as a waiter on the timeout event.
301
+ 1. The timeout schedules a callback to run at time 5.
302
+ 1. The environment takes the event from its `_pending` queue and updates the simulated time to 5.
303
+ 1. The environment runs the callback, which calls `succeed()` on the timeout.
304
+ 1. The timeout calls `resume()` on the process.
305
+ 1. `resume()` schedules an immediate call to `_loop()` with the value `None`.
306
+ 1. `_loop()` calls `send(None)` on the coroutine,
307
+ causing it to advance past the `await`.
308
+ 1. The process prints `"done"` and raises a `StopIteration` exception.
309
+ 1. The process is marked as done.
310
+ 1. Since there are no other events in the pending queue, the environment ends the simulation.
311
+
312
+ ## A Note on Coroutine Adaptation
313
+
314
+ The `ensure_event()` function handles both `Event` objects and bare coroutines.
315
+ For coroutines, it creates a `_Runner` process that `await`s the coroutine
316
+ and then calls `succeed()` on an event with the result.
317
+ This allows `AllOf` and `FirstOf` to accept both events and coroutines.
318
+
319
+ `AllOf` and `FirstOf` must accept coroutines in addition to events
320
+ because of the way Python's `async`/`await` syntax works
321
+ and what users naturally write.
322
+ In the statement:
323
+
324
+ ```python
325
+ await AllOf(env, a=queue.get(), b=resource.acquire())
326
+ ```
327
+
328
+ the expressions `queue.get()` and `resource.acquire()` are calls to `async def` functions.
329
+ In Python,
330
+ calling an async function *does not execute it.
331
+ Instead, it returns a coroutine object.
332
+ If `AllOf` couldn't accept coroutines directly,
333
+ this code would fail because it expects `Event`s.
334
+
335
+ If `AllOf` only accepted events, users would need to write:
336
+
337
+ ```python
338
+ # Manually create events
339
+ evt_a = Event(env)
340
+ evt_b = Event(env)
341
+
342
+ # Manually create runners
343
+ _Runner(env, evt_a, queue.get())
344
+ _Runner(env, evt_b, resource.acquire())
345
+
346
+ # Now use the events
347
+ await AllOf(env, a=evt_a, b=evt_b)
348
+ ```
349
+
350
+ This is verbose and exposes internal implementation details.
351
+
352
+ ## Things I Learned the Hard Way
353
+
354
+ ### Requirements for Correctness
355
+
356
+ `Event` waiter notification must occur before clearing the list.
357
+ : If the list were cleared first, waiters couldn't be resumed.
358
+
359
+ The `_Pending` serial number is necessary.
360
+ : Heap operations require total ordering.
361
+ Without this value,
362
+ events occurring at the same time wouldn't be deterministically ordered,
363
+ which would make simulations irreproducible.
364
+
365
+ Cancelled events must not advance time.
366
+ : The `NO_TIME` sentinel prevents this.
367
+ Without it,
368
+ cancelled timeouts create gaps in the simulation timeline.
369
+
370
+ Process interrupt checking must occur before coroutine sends.
371
+ : This ensures interrupts are handled immediately
372
+ rather than being delayed until the next event.
373
+
374
+ Queue cancellation handlers must remove items or waiters.
375
+ : Without this,
376
+ cancelled `get`s leave processes in the waiters list indefinitely,
377
+ and cancelled items disappear from the queue.
378
+
379
+ Resource cancellation handlers must adjust state.
380
+ : Without them,
381
+ cancelled `acquire`s permanently reduce available capacity or leave ghost waiters.
382
+
383
+ `AllOf` must track completion.
384
+ : Without checking if all events are done, it succeeds prematurely.
385
+
386
+ `FirstOf` must cancel losing events.
387
+ : Otherwise,
388
+ those events remain active and can run later.
389
+
390
+ ### Why Not Just Use Coroutines?
391
+
392
+ [SimPy][simpy] uses bare coroutines.
393
+ [asimpy][asimpy] uses `Event` as the internal primitive for several reasons.
394
+
395
+ Events can be triggered externally.
396
+ : A `Timeout` schedules a callback that later calls `succeed()`.
397
+ A coroutine cannot be "succeeded" from outside: it must run to completion.
398
+
399
+ Events support multiple waiters.
400
+ : Multiple processes can `await` the same event.
401
+ A coroutine can only be awaited once.
402
+
403
+ Events decouple triggering from waiting.
404
+ : The thing that creates an event (like `Timeout.__init__()`)
405
+ is separate from the thing that waits for it.
406
+ With coroutines, creation and execution are more tightly coupled.
407
+
408
+ ### `Event.__await__`
409
+
410
+ `Event.__await__` is defined as:
411
+
412
+ ```python
413
+ def __await__(self):
414
+ value = yield self
415
+ return value
416
+ ```
417
+
418
+ This appears redundant but each part serves a specific purpose in the coroutine protocol.
419
+
420
+ When a coroutine executes `await event`,
421
+ Python calls `event.__await__()`,
422
+ which must return an iterator.
423
+ The `yield self` statement:
424
+
425
+ 1. makes `__await__()` a generator function,
426
+ so it returns a generator (which is a kind of iterator).
427
+ 2. Yields the `Event` object itself up to the `Process`'s `_loop()` method.
428
+
429
+ The `Process` needs the `Event` object so it can call `_add_waiter()` on it:
430
+
431
+ ```python
432
+ def _loop(self, value=None):
433
+ # ...
434
+ yielded = self._coro.send(value) # This receives the Event
435
+ yielded._add_waiter(self) # Register as waiter
436
+ ```
437
+
438
+ Without `yield self`, the `Process` wouldn't know which event to register on.
439
+
440
+ The `value = yield self` statement captures what gets sent back into the generator.
441
+ When the event completes:
442
+
443
+ 1. `Event` calls `proc.resume(value)` .
444
+ 2. `Process` calls `self._loop(value)`.
445
+ 3. `_loop` calls `self._coro.send(value)`.
446
+ 4. This resumes the generator, making `yield self` return `value`.
447
+
448
+ The assignment therefore captures the event's result value.
449
+
450
+ ### Why Return Value
451
+
452
+ The `return value` statement makes that result available to the code that wrote `await event`.
453
+ When a generator returns (via `return` or falling off the end)
454
+ Python raises `StopIteration` with the return value as an attribute.
455
+ The `async`/`await` machinery extracts this and provides it as the result of the `await` expression,
456
+ So when a user writes:
457
+
458
+ ```python
459
+ result = await queue.get()
460
+ ```
461
+
462
+ the flow is:
463
+
464
+ 1. `queue.get()` creates and returns an `Event`.
465
+ 1. `await` calls `Event.__await__()` which yields the `Event` object.
466
+ 1. `Process._loop()` receives the `Event` and registers itself as a waiter.
467
+ 1. Later, the queue calls `event.succeed(item)`.
468
+ 1. `Event` calls `process.resume(item)`.
469
+ 1. `Process` calls `coro.send(item)`.
470
+ 1. The generator resumes, and `yield self` evaluates to `item`.
471
+ 1. The generator executes `return item`.
472
+ 1. `StopIteration(item)` is raised.
473
+ 1. The `async` machinery catches this and makes `await` evaluate to `item`.
474
+
475
+ None of the simpler alternatives would work:
476
+
477
+ - `yield self` alone (no return): the await expression would evaluate to `None`.
478
+ - `return self` (no yield): not a generator, so it violates the iterator protocol.
479
+ - `yield value` then `return value`: the first yield wouldn't provide the `Event` object to the `Process`.
480
+
481
+ [asimpy]: https://asimpy.readthedocs.io/
482
+ [package]: https://pypi.org/project/asimpy/
483
+ [repo]: https://github.com/gvwilson/asimpy
484
+ [simpy]: https://simpy.readthedocs.io/