官术网_书友最值得收藏!

Tasks

Julia has a built-in system for running tasks, which are, in general, known as coroutines. With this, a computation that generates values into a Channel (with a put! function) can be suspended as a task, while a consumer task can pick up the values (with a take! function). This is similar to the yield keyword in Python.

As a concrete example, let's take a look at a fib_producer function that calculates the first 10 Fibonacci numbers (refer to the Recursive functions section in Chapter 3, Functions), but it doesn't return the numbers, it produces them:

# code in Chapter 4\tasks.jl  
 function fib_producer(c::Channel) 
        a, b = (0, 1) 
        for i = 1:10 
            put!(c, b) 
            a, b = (b, a + b) 
        end 
    end 

Construct a Channel by providing this function as an argument:

chnl = Channel(fib_producer)

The task's state is now runnable. To get the Fibonacci numbers, start consuming them with take! until Channel is closed, and the task is finished (state is :done):

take!(chnl) #> 1 
take!(chnl) #> 1 
take!(chnl) #> 2 
take!(chnl) #> 3 
take!(chnl) #> 5 
take!(chnl) #> 8 
take!(chnl) #> 13 
take!(chnl) #> 21 
take!(chnl) #> 34 
take!(chnl) #> 55 
take!(chnl) #> ERROR: InvalidStateException("Channel is closed.", :closed) 

It is as if the fib_producer function was able to return multiple times, once for each take! call. Between calls to fib_producer, its execution is suspended, and the consumer has control.

The same values can be more easily consumed in a for loop, where the loop variable becomes one by one the produced values:

for n in chnl
println(n) end

This produces: 1 1 2 3 5 8 13 21 34 55.

There is a macro @task that does the same thing:

chnl = @task fib_producer(c::Channel)

Coroutines are not executed in different threads, so they cannot run on separate CPUs. Only one coroutine is running at once, but the language runtime switches between them. An internal scheduler controls a queue of runnable tasks and switches between them based on events, such as waiting for data, or data coming in.

Here is another example, which uses @async to start a task asynchronously, binds a channel to the task, and then prints out the contents of the channel:

fac(i::Integer) = (i > 1) ? i*fac(i - 1) : 1 
c = Channel(0) 
task = @async foreach(i->put!(c,fac(i)), 1:5) 
bind(c,task) 
for i in c 
   @show i 
end

This prints out the following:

i = 1
i = 2
i = 6
i = 24
i = 120

Tasks should be seen as a form of cooperative multitasking in a single thread. Switching between tasks does not consume stack space, unlike normal function calls. In general, tasks have very low overhead; so you can use lots of them if needed. Exception handling in Julia is implemented using Tasks as well as servers that accept many incoming connections (refer to the Working with TCP sockets and servers section in Chapter 8, IO, Networking, and Parallel Computing).

True parallelism in Julia is discussed in the Parallel operations and computing section of Chapter 8, IO, Networking, and Parallel Computing.

主站蜘蛛池模板: 武城县| 湟源县| 夏河县| 保德县| 长汀县| 新野县| 连山| 庆阳市| 崇阳县| 沈阳市| 鄂温| 施甸县| 禹州市| 二连浩特市| 游戏| 陇川县| 微山县| 深泽县| 兴宁市| 庆元县| 延川县| 巩留县| 三门县| 黄梅县| 马龙县| 涞水县| 光泽县| 寻乌县| 五指山市| 且末县| 翁牛特旗| 繁昌县| 化德县| 胶州市| 巧家县| 崇信县| 萍乡市| 金湖县| 分宜县| 桐城市| 保定市|