# Places parallelism -- how to return ASAP after 1st answer

**URL:** <https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626>\
**Category:** Questions & Answers\
**Created:** [January 7, 2023, 2:13pm UTC](https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626 "2023-01-07T14:13:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rscho](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/rscho/32/726_2.png) [@rscho](https://racket.discourse.group/u/rscho)\
**Post date:** [January 7, 2023, 2:13pm UTC](https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626/1 "2023-01-07T14:13:42Z")

</div>

Hi,

Taking the example program for places from the [docs](https://docs.racket-lang.org/reference/places.html):

```scheme
(let ([pls (for/list ([i (in-range 2)])
              (dynamic-place "place-worker.rkt" 'place-main))])
   (for ([i (in-range 2)]
         [p pls])
      (place-channel-put p i)
      (printf "~a\n" (place-channel-get p)))
   (map place-wait pls))

```

How can I return early, as soon as the 1st message gets back and kill all other places having not returned yet? I'm doing a lengthy computation involving a random search, that I would like to speed up.

Thanks!

---

<div class="post-metadata">

**Author:** ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)\
**Post date:** [January 7, 2023, 5:57pm UTC](https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626/2 "2023-01-07T17:57:48Z")

</div>

> A [place channel](https://docs.racket-lang.org/reference/places.html#%28tech._place._channel%29) can be used as a [synchronizable event](https://docs.racket-lang.org/reference/sync.html#%28tech._synchronizable._event%29) (see [Events](https://docs.racket-lang.org/reference/sync.html)) to receive a value through the channel. A [place channel](https://docs.racket-lang.org/reference/places.html#%28tech._place._channel%29) is [ready for synchronization](https://docs.racket-lang.org/reference/sync.html#%28tech._ready._for._synchronization%29) when a message is available on the channel, and the [place channel](https://docs.racket-lang.org/reference/places.html#%28tech._place._channel%29)’s [synchronization result](https://docs.racket-lang.org/reference/sync.html#%28tech._synchronization._result%29) is the message (which is removed on synchronization).

```scheme
(let ([pls (for/list ([i (in-range 2)])
              (dynamic-place "place-worker.rkt" 'place-main))])
   (define result (apply sync pls))
   (map place-kill pls)
   (do-something-with result))

```

Apply is used with sync and returns with the result from one of the places.  
(sync with multiple arguments (via apply) waits on those arguments and returns with one of them (when there is one that is ready))  
Map kills all the places (if they aren't already dead).  
`do-something-with` is a stub replace it with something or alternatively just return `result` as the expression's result from the let block.  
_Haven't tested it, but should work, does this do what you want?_

If this is part of a larger program it might be a good idea to setup a custodian to make sure everything is cleaned up when you are done searching for stuff, alternatively you could use exception handlers or dynamic-wind.

---

<div class="post-metadata">

**Author:** ![rscho](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/rscho/32/726_2.png) [@rscho](https://racket.discourse.group/u/rscho)\
**Post date:** [January 7, 2023, 8:56pm UTC](https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626/3 "2023-01-07T20:56:32Z")

</div>

Works perfectly! Thanks for the tips, I think I would've taken a long time to understand `sync` from reading the docs.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [January 9, 2023, 2:36pm UTC](https://racket.discourse.group/t/places-parallelism-how-to-return-asap-after-1st-answer/1626/4 "2023-01-09T14:36:27Z")

</div>

One nit: I prefer `(for-each place-kill pls)` (since we don't do anything with the results of `place-kill`).
