Do you mean that you can't have multiple instances of do_something_with
running at the same time on different inputs? One approach would be to put
a lock around that call, e.g. something like this:

julia> @time @sync for i = 1:20
           @async begin
               t = 3*rand()
               sleep(t)
               lock(lk)
               print("at $i ")
               sleep(0.1)
               println("slept $t")
               unlock(lk)
           end
       end
at 6 slept 0.20118848920199706
at 11 slept 0.21942339919536646
at 4 slept 0.41515354023835105
at 12 slept 0.5548132885524406
at 14 slept 0.8882682174479226
at 18 slept 0.9508509696738914
at 2 slept 1.0564180777734677
at 16 slept 1.3294530172731525
at 1 slept 1.371671593498631
at 20 slept 1.415684746758686
at 8 slept 1.6166489432453863
at 13 slept 1.8970444898780345
at 5 slept 1.9215629456206693
at 7 slept 2.212396096460613
at 9 slept 2.421181780343373
at 10 slept 2.658124287417618
at 15 slept 2.677429475002988
at 19 slept 2.749139595074486
at 17 slept 2.9231737427121964
at 3 slept 2.926315297149849
  3.189635 seconds (2.21 k allocations: 200.336 KB)


As you can see, the sleep regions before the lock are executed concurrently
while the printing parts are each executed "atomically" in the sense that
tasks don't interrupt each other printing despite the sleep in the middle.

On Wed, Feb 3, 2016 at 11:04 AM, STAR0SS <yu...@altern.org> wrote:

> I have something of the sort:
>
> #some loop
>
>     ...
>
>     s = get_data_from_pipe()
>     do_something_with(s)
>
>
> I need do_something_with to happen synchronously within the loop, so I
> cannot run that in a Task, and if get_data_from_pipe blocks then it kills
> my loop.
>
> What I did is that I have a Task that reads from the Pipe and puts the
> data in an IOBuffer, then read from that in the loop instead, it seems to
> work.
>

Reply via email to