If you want to pause after every i-th iteration, you can wrap the cycle in enumerate
.
for (i, x) in enumerate(Iterators.cycle(a))
cont[] || break
println(x)
# Sleep after every fifth iteration
(i % 5 == 0) && sleep(5)
end
prints
1 a
2 b
3 c
4 a
5 b
6 c
7 a
[...]
If you want to pause after processing every i-th element of your input array a
, then you can just swap enumerate
and Iterators.cycle
(i.e., Iterators.cycle(enumerate(a))
) which will produce pairs (i, a[i])
forever.
I also see that you sketched something that looks like batch processing (a[count:count+n]
). If that’s your intention, you can use Iterators.partition
for that.
For example:
julia> a = ["a", "b", "c", "d", "e", "f", "g"];
julia> function test(a)
cont = Ref(true)
# Use batches of maximum size 3
batches = Iterators.partition(A, 3)
@async for (i, x) in enumerate(Iterators.cycle(batches))
cont[] || break
println(i, ' ', x)
if i % 2 == 0
println("Sleeping...")
sleep(2)
end
end
return cont
end
test (generic function with 1 method)
julia> c = test(a);
prints
1 ["a", "b", "c"]
2 ["d", "e", "f"]
Sleeping...
3 ["g"]
4 ["a", "b", "c"]
Sleeping...
5 ["d", "e", "f"]
6 ["g"]
Sleeping...
7 ["a", "b", "c"]
8 ["d", "e", "f"]
Sleeping...
9 ["g"]
10 ["a", "b", "c"]
Sleeping...
11 ["d", "e", "f"]
12 ["g"]
Sleeping...
13 ["a", "b", "c"]
14 ["d", "e", "f"]
Sleeping...
[...]
It processes the array in batches of size 3, and after processing every second batch, it sleeps.