Stop Thread
Here's an example agent with an extra card in +on-poke to stop the thread and a little extra in +on-agent to print things for demonstrative purposes. We run the thread by poking Spider directly because we need to know the thread ID in order to stop it. In this example we use a thread file, but an inline thread could also be used.
We've added a sleep to the thread to keep it running for demonstration:
/+ *strandio
|= arg=vase
=/ m (strand:rand ,vase)
^- form:m
;< =path bind:m take-watch
;< ~ bind:m (send-raw-card [%give %fact ~[path] %update !>("message 1")])
;< ~ bind:m %- send-raw-cards
:~ [%give %fact ~[path] %update !>("message 2")]
[%give %fact ~[path] %update !>("message 3")]
[%give %fact ~[path] %update !>("message 4")]
==
;< ~ bind:m (send-raw-card [%give %kick ~[path] ~])
;< ~ bind:m (sleep ~m1)
|= strand-input:rand
?+ q.arg [~ %fail %not-foo ~]
%foo
[~ %done arg]
==Save these, |commit, and run with :thread-starter [%test-thread %foo]. You should see:
Thread started successfully
message 1
message 2
message 3
message 4
Thread cancelled successfully
Thread cancelled nicelyNow, try changing the vase in our new card from !>([tid %.y]) to !>([tid %.n]). Save, |commit, and run again. You should see:
Thread started successfully
message 1
message 2
message 3
message 4
Thread cancelled successfully
Thread failed: cancelledAnalysis
The card we've added to our agent:
[%pass /thread-stop/[ta-now] %agent [our.bowl %spider] %poke %spider-stop !>([tid %.y])]...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.
Last updated