Skip to content

Commit 4f4f43e

Browse files
authored
Merge pull request #677 from Point72/tkp/docs
Add getting started faqs
2 parents 4c222e8 + 8627f37 commit 4f4f43e

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

docs/wiki/_Sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Notes for editors:
2424
- [Execution Modes](Execution-Modes)
2525
- [Adapters](Adapters)
2626
- [Feedback and Delayed Edge](Feedback-and-Delayed-Edge)
27-
- [Common Mistakes](Common-Mistakes)
27+
- [Common Mistakes / FAQ](Common-Mistakes)
2828

2929
**How-to guides**
3030

docs/wiki/concepts/Common-Mistakes.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
## Table of Contents
1+
When starting out with CSP, some behaviors can be unfamiliar and/or unexpected.
2+
We are collecting common questions and scenarios on this page, with explanations and, if applicable, corrected code.
23

34
- [Graphs vs. nodes](#graphs-vs-nodes)
45
- [Always use engine time](#always-use-engine-time)
6+
- [`csp.output` vs `return`](#cspoutput-vs-return)
57

68
## Graphs vs. nodes
79

@@ -21,7 +23,7 @@ def square(x: ts[int]) -> ts[int]:
2123

2224
@csp.node
2325
def cube(x: ts[int]) -> ts[int]:
24-
return x*square(x)
26+
return x*square(x)
2527

2628

2729
csp.run(cube(csp.const(1)), starttime=datetime(2020,1,1), endtime=timedelta())
@@ -35,7 +37,7 @@ Corrected code - we simply make `cube` a graph, not a node.
3537
```python
3638
@csp.graph
3739
def cube(x: ts[int]) -> ts[int]:
38-
return x*square(x)
40+
return x*square(x)
3941
```
4042

4143
2. *Graph code does not access runtime values*. This is the inverse of (1): graph code treats all time-series as Edges and simply "wires" together the application.
@@ -91,3 +93,44 @@ Corrected code - we use `csp.now` instead of `datetime.now` so the node works on
9193
if time >= csp.now():
9294
...
9395
```
96+
97+
## `csp.output` vs `return`
98+
99+
Sequencing of ticks and returns can be important, and `return` semantics
100+
are the same in CSP as Python generally.
101+
In the below example, if `x` ticks at the same time as `alarm`, the second `if` block will not execute.
102+
103+
```python
104+
105+
@csp.node
106+
def my_node(x: ts[float]) -> ts[float]:
107+
with csp.alarms():
108+
alarm = csp.alarm(bool)
109+
with csp.start():
110+
csp.schedule_alarm(alarm, timedelta(seconds=1), True)
111+
if csp.ticked(x):
112+
print("input ticked")
113+
return x
114+
if csp.ticked(alarm):
115+
csp.schedule_alarm(alarm, timedelta(seconds=10), True)
116+
print("alarm ticked")
117+
return x
118+
119+
```
120+
121+
If you want node execution to continue after setting an output's value, you can instead use `csp.output` to emit, but not end Python control flow.
122+
123+
```python
124+
@csp.node
125+
def my_node(x: ts[float]) -> ts[float]:
126+
with csp.alarms():
127+
alarm = csp.alarm(bool)
128+
with csp.start():
129+
csp.schedule_alarm(alarm, timedelta(seconds=1), True)
130+
if csp.ticked(x):
131+
print("input ticked")
132+
csp.output(x)
133+
if csp.ticked(alarm):
134+
csp.schedule_alarm(alarm, timedelta(seconds=1), True)
135+
print("alarm ticked")
136+
```

0 commit comments

Comments
 (0)