7Here we've added one last card to `on-poke` to stop the thread and a little extra to `on-agent` to print things for demonstrative purposes.
9#### `thread-starter.hoon`
13=* card card:agent:gall
18 def ~(. (default-agent this %|) bowl)
26 ?+ mark (on-poke:def mark vase)
28 ?+ q.vase (on-poke:def mark vase)
30 =/ tid `@ta`(cat 3 'thread_' (scot %uv (sham eny.bowl)))
31 =/ ta-now `@ta`(scot %da now.bowl)
32 =/ start-args [~ `tid byk.bowl p.q.vase !>(q.q.vase)]
35 [%pass /thread/[ta-now] %agent [our.bowl %spider] %watch /thread-result/[tid]]
36 [%pass /thread/[ta-now] %agent [our.bowl %spider] %poke %spider-start !>(start-args)]
37 [%pass /thread/updates/[ta-now] %agent [our.bowl %spider] %watch /thread/[tid]/updates]
38 [%pass /thread-stop/[ta-now] %agent [our.bowl %spider] %poke %spider-stop !>([tid %.y])]
42++ on-watch on-watch:def
43++ on-leave on-leave:def
46 |= [=wire =sign:agent:gall]
48 ?+ -.wire (on-agent:def wire sign)
50 ?+ -.sign (on-agent:def wire sign)
53 %- (slog leaf+"Thread started successfully" ~)
55 %- (slog leaf+"Thread failed to start" u.p.sign)
59 ?+ p.cage.sign (on-agent:def wire sign)
61 =/ err !< (pair term tang) q.cage.sign
62 %- (slog leaf+"Thread failed: {(trip p.err)}" q.err)
65 ?: =(q.cage.sign *vase)
66 %- (slog leaf+"Thread cancelled nicely" ~)
68 =/ res (trip !<(term q.cage.sign))
69 %- (slog leaf+"Result: {res}" ~)
72 =/ msg !< tape q.cage.sign
78 ?+ -.sign (on-agent:def wire sign)
81 %- (slog leaf+"Thread cancelled successfully" ~)
83 %- (slog leaf+"Thread failed to stop" u.p.sign)
92We've also added a `sleep` to the thread to keep it running for demonstration.
94#### `test-thread.hoon`
99=, strand=strand:spider
104;< =path bind:m take-watch
105;< ~ bind:m (send-raw-card [%give %fact ~[path] %update !>("message 1")])
106;< ~ bind:m %- send-raw-cards
107 :~ [%give %fact ~[path] %update !>("message 2")]
108 [%give %fact ~[path] %update !>("message 3")]
109 [%give %fact ~[path] %update !>("message 4")]
111;< ~ bind:m (send-raw-card [%give %kick ~[path] ~])
112;< ~ bind:m (sleep ~m1)
113|= strand-input:strand
114?+ q.arg [~ %fail %not-foo ~]
120Save these, `|commit`, and run with `:thread-starter [%test-thread %foo]`. You should see:
123Thread started successfully
128Thread cancelled successfully
129Thread cancelled nicely
132Now, try changing the vase in our new card from `!>([tid %.y])` to `!>([tid %.n])`. Save, `|commit`, and run again. You should see:
135Thread started successfully
140Thread cancelled successfully
141Thread failed: cancelled
146The card we've added to our agent:
149[%pass /thread-stop/[ta-now] %agent [our.bowl %spider] %poke %spider-stop !>([tid %.y])]
152...pokes spider with mark `%spider-stop` and a vase containing the tid of the thread we want to stop and a `?`. The `?` specifies whether to end it nicely or not. If `%.y` it will end with `%thread-done` and a `*vase` bunted vase. If `%.n` it will end with `%thread-fail` and a vase containing `[term tang]` where `term` is `%cancelled` and `tang` is `~`. You can see the difference in our tests above.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list