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

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.

主站蜘蛛池模板: 保亭| 彭山县| 宜兰市| 盐城市| 阿图什市| 呼和浩特市| 姚安县| 上饶市| 紫阳县| 辽阳市| 荔浦县| 永定县| 栾城县| 平顺县| 嘉善县| 屯昌县| 嫩江县| 石楼县| 永登县| 韩城市| 托克逊县| 宁阳县| 康马县| 信阳市| 静乐县| 绿春县| 绥阳县| 东乡| 镇平县| 民乐县| 西丰县| 永仁县| 芜湖县| 安塞县| 山阴县| 德阳市| 阳信县| 定西市| 崇阳县| 沿河| 年辖:市辖区|