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

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.

主站蜘蛛池模板: 隆林| 绥阳县| 博客| 麟游县| 定兴县| 莒南县| 福贡县| 巴楚县| 淮北市| 栖霞市| 吉木萨尔县| 满洲里市| 衢州市| 阿拉善右旗| 苏州市| 鸡泽县| 镇远县| 沈丘县| 张北县| 乐清市| 巴里| 瓦房店市| 海晏县| 义乌市| 三台县| 本溪市| 泸定县| 连江县| 晋中市| 镇雄县| 泰安市| 苏尼特右旗| 磴口县| 九寨沟县| 海淀区| 平和县| 雅江县| 墨竹工卡县| 彩票| 海伦市| 溧水县|