陳慶全
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 流程控制 Julia provides a variety of control flow constructs: * [Compound Expressions](@ref man-compound-expressions): `begin` and `(;)`. * [Conditional Evaluation](@ref man-conditional-evaluation): `if`-`elseif`-`else` and `?:` (ternary operator). * [Short-Circuit Evaluation](@ref): `&&`, `||` and chained comparisons. * [Repeated Evaluation: Loops](@ref man-loops): `while` and `for`. * [Exception Handling](@ref): `try`-`catch`, [`error()`](@ref) and [`throw()`](@ref). * [Tasks (aka Coroutines)](@ref man-tasks): [`yieldto()`](@ref). The first five control flow mechanisms are standard to high-level programming languages. [`Task`](@ref)s are not so standard: they provide non-local control flow, making it possible to switch between temporarily-suspended computations. This is a powerful construct: both exception handling and cooperative multitasking are implemented in Julia using tasks. Everyday programming requires no direct usage of tasks, but certain problems can be solved much more easily by using tasks. ## [Compound Expressions](@id man-compound-expressions) Sometimes it is convenient to have a single expression which evaluates several subexpressions in order, returning the value of the last subexpression as its value. There are two Julia constructs that accomplish this: `begin` blocks and `(;)` chains. The value of both compound expression constructs is that of the last subexpression. Here's an example of a `begin` block: ```jldoctest julia> z = begin x = 1 y = 2 x + y end 3 ``` Since these are fairly small, simple expressions, they could easily be placed onto a single line, which is where the `(;)` chain syntax comes in handy: ```jldoctest julia> z = (x = 1; y = 2; x + y) 3 ``` This syntax is particularly useful with the terse single-line function definition form introduced in [Functions](@ref). Although it is typical, there is no requirement that `begin` blocks be multiline or that `(;)` chains be single-line: ```jldoctest julia> begin x = 1; y = 2; x + y end 3 julia> (x = 1; y = 2; x + y) 3 ``` ## [Conditional Evaluation](@id man-conditional-evaluation) Conditional evaluation allows portions of code to be evaluated or not evaluated depending on the value of a boolean expression. Here is the anatomy of the `if`-`elseif`-`else` conditional syntax: ```julia if x < y println("x is less than y") elseif x > y println("x is greater than y") else println("x is equal to y") end ``` If the condition expression `x < y` is `true`, then the corresponding block is evaluated; otherwise the condition expression `x > y` is evaluated, and if it is `true`, the corresponding block is evaluated; if neither expression is true, the `else` block is evaluated. Here it is in action: ```jldoctest julia> function test(x, y) if x < y println("x is less than y") elseif x > y println("x is greater than y") else println("x is equal to y") end end test (generic function with 1 method) julia> test(1, 2) x is less than y julia> test(2, 1) x is greater than y julia> test(1, 1) x is equal to y ``` The `elseif` and `else` blocks are optional, and as many `elseif` blocks as desired can be used. The condition expressions in the `if`-`elseif`-`else` construct are evaluated until the first one evaluates to `true`, after which the associated block is evaluated, and no further condition expressions or blocks are evaluated. `if` blocks are "leaky", i.e. they do not introduce a local scope. This means that new variables defined inside the `if` clauses can be used after the `if` block, even if they weren't defined before. So, we could have defined the `test` function above as ```jldoctest julia> function test(x,y) if x < y relation = "less than" elseif x == y relation = "equal to" else relation = "greater than" end println("x is ", relation, " y.") end test (generic function with 1 method) julia> test(2, 1) x is greater than y. ``` The variable `relation` is declared inside the `if` block, but used outside. However, when depending on this behavior, make sure all possible code paths define a value for the variable. The following change to the above function results in a runtime error ```jldoctest julia> function test(x,y) if x < y relation = "less than" elseif x == y relation = "equal to" end println("x is ", relation, " y.") end test (generic function with 1 method) julia> test(1,2) x is less than y. julia> test(2,1) ERROR: UndefVarError: relation not defined Stacktrace: [1] test(::Int64, ::Int64) at ./none:7 ``` `if` blocks also return a value, which may seem unintuitive to users coming from many other languages. This value is simply the return value of the last executed statement in the branch that was chosen, so ```jldoctest julia> x = 3 3 julia> if x > 0 "positive!" else "negative..." end "positive!" ``` Note that very short conditional statements (one-liners) are frequently expressed using Short-Circuit Evaluation in Julia, as outlined in the next section. Unlike C, MATLAB, Perl, Python, and Ruby -- but like Java, and a few other stricter, typed languages -- it is an error if the value of a conditional expression is anything but `true` or `false`: ```jldoctest julia> if 1 println("true") end ERROR: TypeError: non-boolean (Int64) used in boolean context ``` This error indicates that the conditional was of the wrong type: `Int64` rather than the required `Bool`. The so-called "ternary operator", `?:`, is closely related to the `if`-`elseif`-`else` syntax, but is used where a conditional choice between single expression values is required, as opposed to conditional execution of longer blocks of code. It gets its name from being the only operator in most languages taking three operands: ```julia a ? b : c ``` The expression `a`, before the `?`, is a condition expression, and the ternary operation evaluates the expression `b`, before the `:`, if the condition `a` is `true` or the expression `c`, after the `:`, if it is `false`. The easiest way to understand this behavior is to see an example. In the previous example, the `println` call is shared by all three branches: the only real choice is which literal string to print. This could be written more concisely using the ternary operator. For the sake of clarity, let's try a two-way version first: ```jldoctest julia> x = 1; y = 2; julia> println(x < y ? "less than" : "not less than") less than julia> x = 1; y = 0; julia> println(x < y ? "less than" : "not less than") not less than ``` If the expression `x < y` is true, the entire ternary operator expression evaluates to the string `"less than"` and otherwise it evaluates to the string `"not less than"`. The original three-way example requires chaining multiple uses of the ternary operator together: ```jldoctest julia> test(x, y) = println(x < y ? "x is less than y" : x > y ? "x is greater than y" : "x is equal to y") test (generic function with 1 method) julia> test(1, 2) x is less than y julia> test(2, 1) x is greater than y julia> test(1, 1) x is equal to y ``` To facilitate chaining, the operator associates from right to left. It is significant that like `if`-`elseif`-`else`, the expressions before and after the `:` are only evaluated if the condition expression evaluates to `true` or `false`, respectively: ```jldoctest julia> v(x) = (println(x); x) v (generic function with 1 method) julia> 1 < 2 ? v("yes") : v("no") yes "yes" julia> 1 > 2 ? v("yes") : v("no") no "no" ``` ## Short-Circuit Evaluation Short-circuit evaluation is quite similar to conditional evaluation. The behavior is found in most imperative programming languages having the `&&` and `||` boolean operators: in a series of boolean expressions connected by these operators, only the minimum number of expressions are evaluated as are necessary to determine the final boolean value of the entire chain. Explicitly, this means that: * In the expression `a && b`, the subexpression `b` is only evaluated if `a` evaluates to `true`. * In the expression `a || b`, the subexpression `b` is only evaluated if `a` evaluates to `false`. The reasoning is that `a && b` must be `false` if `a` is `false`, regardless of the value of `b`, and likewise, the value of `a || b` must be true if `a` is `true`, regardless of the value of `b`. Both `&&` and `||` associate to the right, but `&&` has higher precedence than `||` does. It's easy to experiment with this behavior: ```jldoctest tandf julia> t(x) = (println(x); true) t (generic function with 1 method) julia> f(x) = (println(x); false) f (generic function with 1 method) julia> t(1) && t(2) 1 2 true julia> t(1) && f(2) 1 2 false julia> f(1) && t(2) 1 false julia> f(1) && f(2) 1 false julia> t(1) || t(2) 1 true julia> t(1) || f(2) 1 true julia> f(1) || t(2) 1 2 true julia> f(1) || f(2) 1 2 false ``` You can easily experiment in the same way with the associativity and precedence of various combinations of `&&` and `||` operators. This behavior is frequently used in Julia to form an alternative to very short `if` statements. Instead of `if <cond> <statement> end`, one can write `<cond> && <statement>` (which could be read as: <cond> *and then* <statement>). Similarly, instead of `if ! <cond> <statement> end`, one can write `<cond> || <statement>` (which could be read as: <cond> *or else* <statement>). For example, a recursive factorial routine could be defined like this: ```jldoctest julia> function fact(n::Int) n >= 0 || error("n must be non-negative") n == 0 && return 1 n * fact(n-1) end fact (generic function with 1 method) julia> fact(5) 120 julia> fact(0) 1 julia> fact(-1) ERROR: n must be non-negative Stacktrace: [1] fact(::Int64) at ./none:2 ``` Boolean operations *without* short-circuit evaluation can be done with the bitwise boolean operators introduced in [Mathematical Operations and Elementary Functions](@ref): `&` and `|`. These are normal functions, which happen to support infix operator syntax, but always evaluate their arguments: ```jldoctest tandf julia> f(1) & t(2) 1 2 false julia> t(1) | t(2) 1 2 true ``` Just like condition expressions used in `if`, `elseif` or the ternary operator, the operands of `&&` or `||` must be boolean values (`true` or `false`). Using a non-boolean value anywhere except for the last entry in a conditional chain is an error: ```jldoctest julia> 1 && true ERROR: TypeError: non-boolean (Int64) used in boolean context ``` On the other hand, any type of expression can be used at the end of a conditional chain. It will be evaluated and returned depending on the preceding conditionals: ```jldoctest julia> true && (x = (1, 2, 3)) (1, 2, 3) julia> false && (x = (1, 2, 3)) false ``` ## [Repeated Evaluation: Loops](@id man-loops) There are two constructs for repeated evaluation of expressions: the `while` loop and the `for` loop. Here is an example of a `while` loop: ```jldoctest julia> i = 1; julia> while i <= 5 println(i) i += 1 end 1 2 3 4 5 ``` The `while` loop evaluates the condition expression (`i <= 5` in this case), and as long it remains `true`, keeps also evaluating the body of the `while` loop. If the condition expression is `false` when the `while` loop is first reached, the body is never evaluated. The `for` loop makes common repeated evaluation idioms easier to write. Since counting up and down like the above `while` loop does is so common, it can be expressed more concisely with a `for` loop: ```jldoctest julia> for i = 1:5 println(i) end 1 2 3 4 5 ``` Here the `1:5` is a `Range` object, representing the sequence of numbers 1, 2, 3, 4, 5. The `for` loop iterates through these values, assigning each one in turn to the variable `i`. One rather important distinction between the previous `while` loop form and the `for` loop form is the scope during which the variable is visible. If the variable `i` has not been introduced in an other scope, in the `for` loop form, it is visible only inside of the `for` loop, and not afterwards. You'll either need a new interactive session instance or a different variable name to test this: ```jldoctest julia> for j = 1:5 println(j) end 1 2 3 4 5 julia> j ERROR: UndefVarError: j not defined ``` See [Scope of Variables](@ref scope-of-variables) for a detailed explanation of variable scope and how it works in Julia. In general, the `for` loop construct can iterate over any container. In these cases, the alternative (but fully equivalent) keyword `in` or `∈` is typically used instead of `=`, since it makes the code read more clearly: ```jldoctest julia> for i in [1,4,0] println(i) end 1 4 0 julia> for s ∈ ["foo","bar","baz"] println(s) end foo bar baz ``` Various types of iterable containers will be introduced and discussed in later sections of the manual (see, e.g., [Multi-dimensional Arrays](@ref man-multi-dim-arrays)). It is sometimes convenient to terminate the repetition of a `while` before the test condition is falsified or stop iterating in a `for` loop before the end of the iterable object is reached. This can be accomplished with the `break` keyword: ```jldoctest julia> i = 1; julia> while true println(i) if i >= 5 break end i += 1 end 1 2 3 4 5 julia> for i = 1:1000 println(i) if i >= 5 break end end 1 2 3 4 5 ``` Without the `break` keyword, the above `while` loop would never terminate on its own, and the `for` loop would iterate up to 1000. These loops are both exited early by using `break`. In other circumstances, it is handy to be able to stop an iteration and move on to the next one immediately. The `continue` keyword accomplishes this: ```jldoctest julia> for i = 1:10 if i % 3 != 0 continue end println(i) end 3 6 9 ``` This is a somewhat contrived example since we could produce the same behavior more clearly by negating the condition and placing the `println` call inside the `if` block. In realistic usage there is more code to be evaluated after the `continue`, and often there are multiple points from which one calls `continue`. Multiple nested `for` loops can be combined into a single outer loop, forming the cartesian product of its iterables: ```jldoctest julia> for i = 1:2, j = 3:4 println((i, j)) end (1, 3) (1, 4) (2, 3) (2, 4) ``` A `break` statement inside such a loop exits the entire nest of loops, not just the inner one. ## Exception Handling When an unexpected condition occurs, a function may be unable to return a reasonable value to its caller. In such cases, it may be best for the exceptional condition to either terminate the program, printing a diagnostic error message, or if the programmer has provided code to handle such exceptional circumstances, allow that code to take the appropriate action. ### Built-in `Exception`s `Exception`s are thrown when an unexpected condition has occurred. The built-in `Exception`s listed below all interrupt the normal flow of control. | `Exception` | |:----------------------------- | | [`ArgumentError`](@ref) | | [`BoundsError`](@ref) | | `CompositeException` | | [`DivideError`](@ref) | | [`DomainError`](@ref) | | [`EOFError`](@ref) | | [`ErrorException`](@ref) | | [`InexactError`](@ref) | | [`InitError`](@ref) | | [`InterruptException`](@ref) | | `InvalidStateException` | | [`KeyError`](@ref) | | [`LoadError`](@ref) | | [`OutOfMemoryError`](@ref) | | [`ReadOnlyMemoryError`](@ref) | | [`RemoteException`](@ref) | | [`MethodError`](@ref) | | [`OverflowError`](@ref) | | [`ParseError`](@ref) | | [`SystemError`](@ref) | | [`TypeError`](@ref) | | [`UndefRefError`](@ref) | | [`UndefVarError`](@ref) | | `UnicodeError` | For example, the [`sqrt()`](@ref) function throws a [`DomainError`](@ref) if applied to a negative real value: ```jldoctest julia> sqrt(-1) ERROR: DomainError: sqrt will only return a complex result if called with a complex argument. Try sqrt(complex(x)). Stacktrace: [1] sqrt(::Int64) at ./math.jl:431 ``` You may define your own exceptions in the following way: ```jldoctest julia> struct MyCustomException <: Exception end ``` ### The [`throw()`](@ref) function Exceptions can be created explicitly with [`throw()`](@ref). For example, a function defined only for nonnegative numbers could be written to [`throw()`](@ref) a [`DomainError`](@ref) if the argument is negative: ```jldoctest julia> f(x) = x>=0 ? exp(-x) : throw(DomainError()) f (generic function with 1 method) julia> f(1) 0.36787944117144233 julia> f(-1) ERROR: DomainError: Stacktrace: [1] f(::Int64) at ./none:1 ``` Note that [`DomainError`](@ref) without parentheses is not an exception, but a type of exception. It needs to be called to obtain an `Exception` object: ```jldoctest julia> typeof(DomainError()) <: Exception true julia> typeof(DomainError) <: Exception false ``` Additionally, some exception types take one or more arguments that are used for error reporting: ```jldoctest julia> throw(UndefVarError(:x)) ERROR: UndefVarError: x not defined ``` This mechanism can be implemented easily by custom exception types following the way [`UndefVarError`](@ref) is written: ```jldoctest julia> struct MyUndefVarError <: Exception var::Symbol end julia> Base.showerror(io::IO, e::MyUndefVarError) = print(io, e.var, " not defined") ``` !!! note When writing an error message, it is preferred to make the first word lowercase. For example, `size(A) == size(B) || throw(DimensionMismatch("size of A not equal to size of B"))` is preferred over `size(A) == size(B) || throw(DimensionMismatch("Size of A not equal to size of B"))`. However, sometimes it makes sense to keep the uppercase first letter, for instance if an argument to a function is a capital letter: `size(A,1) == size(B,2) || throw(DimensionMismatch("A has first dimension..."))`. ### Errors The [`error()`](@ref) function is used to produce an [`ErrorException`](@ref) that interrupts the normal flow of control. Suppose we want to stop execution immediately if the square root of a negative number is taken. To do this, we can define a fussy version of the [`sqrt()`](@ref) function that raises an error if its argument is negative: ```jldoctest fussy_sqrt julia> fussy_sqrt(x) = x >= 0 ? sqrt(x) : error("negative x not allowed") fussy_sqrt (generic function with 1 method) julia> fussy_sqrt(2) 1.4142135623730951 julia> fussy_sqrt(-1) ERROR: negative x not allowed Stacktrace: [1] fussy_sqrt(::Int64) at ./none:1 ``` If `fussy_sqrt` is called with a negative value from another function, instead of trying to continue execution of the calling function, it returns immediately, displaying the error message in the interactive session: ```jldoctest fussy_sqrt julia> function verbose_fussy_sqrt(x) println("before fussy_sqrt") r = fussy_sqrt(x) println("after fussy_sqrt") return r end verbose_fussy_sqrt (generic function with 1 method) julia> verbose_fussy_sqrt(2) before fussy_sqrt after fussy_sqrt 1.4142135623730951 julia> verbose_fussy_sqrt(-1) before fussy_sqrt ERROR: negative x not allowed Stacktrace: [1] fussy_sqrt at ./none:1 [inlined] [2] verbose_fussy_sqrt(::Int64) at ./none:3 ``` ### Warnings and informational messages Julia also provides other functions that write messages to the standard error I/O, but do not throw any `Exception`s and hence do not interrupt execution: ```jldoctest julia> info("Hi"); 1+1 INFO: Hi 2 julia> warn("Hi"); 1+1 WARNING: Hi 2 julia> error("Hi"); 1+1 ERROR: Hi Stacktrace: [1] error(::String) at ./error.jl:21 ``` ### The `try/catch` statement The `try/catch` statement allows for `Exception`s to be tested for. For example, a customized square root function can be written to automatically call either the real or complex square root method on demand using `Exception`s : ```jldoctest julia> f(x) = try sqrt(x) catch sqrt(complex(x, 0)) end f (generic function with 1 method) julia> f(1) 1.0 julia> f(-1) 0.0 + 1.0im ``` It is important to note that in real code computing this function, one would compare `x` to zero instead of catching an exception. The exception is much slower than simply comparing and branching. `try/catch` statements also allow the `Exception` to be saved in a variable. In this contrived example, the following example calculates the square root of the second element of `x` if `x` is indexable, otherwise assumes `x` is a real number and returns its square root: ```jldoctest julia> sqrt_second(x) = try sqrt(x[2]) catch y if isa(y, DomainError) sqrt(complex(x[2], 0)) elseif isa(y, BoundsError) sqrt(x) end end sqrt_second (generic function with 1 method) julia> sqrt_second([1 4]) 2.0 julia> sqrt_second([1 -4]) 0.0 + 2.0im julia> sqrt_second(9) 3.0 julia> sqrt_second(-9) ERROR: DomainError: Stacktrace: [1] sqrt_second(::Int64) at ./none:7 ``` Note that the symbol following `catch` will always be interpreted as a name for the exception, so care is needed when writing `try/catch` expressions on a single line. The following code will *not* work to return the value of `x` in case of an error: ```julia try bad() catch x end ``` Instead, use a semicolon or insert a line break after `catch`: ```julia try bad() catch; x end try bad() catch x end ``` The `catch` clause is not strictly necessary; when omitted, the default return value is `nothing`. ```jldoctest julia> try error() end # Returns nothing ``` The power of the `try/catch` construct lies in the ability to unwind a deeply nested computation immediately to a much higher level in the stack of calling functions. There are situations where no error has occurred, but the ability to unwind the stack and pass a value to a higher level is desirable. Julia provides the [`rethrow()`](@ref), [`backtrace()`](@ref) and [`catch_backtrace()`](@ref) functions for more advanced error handling. ### `finally` Clauses In code that performs state changes or uses resources like files, there is typically clean-up work (such as closing files) that needs to be done when the code is finished. Exceptions potentially complicate this task, since they can cause a block of code to exit before reaching its normal end. The `finally` keyword provides a way to run some code when a given block of code exits, regardless of how it exits. For example, here is how we can guarantee that an opened file is closed: ```julia f = open("file") try # operate on file f finally close(f) end ``` When control leaves the `try` block (for example due to a `return`, or just finishing normally), `close(f)` will be executed. If the `try` block exits due to an exception, the exception will continue propagating. A `catch` block may be combined with `try` and `finally` as well. In this case the `finally` block will run after `catch` has handled the error. ## [Tasks (aka Coroutines)](@id man-tasks) Tasks are a control flow feature that allows computations to be suspended and resumed in a flexible manner. This feature is sometimes called by other names, such as symmetric coroutines, lightweight threads, cooperative multitasking, or one-shot continuations. When a piece of computing work (in practice, executing a particular function) is designated as a [`Task`](@ref), it becomes possible to interrupt it by switching to another [`Task`](@ref). The original [`Task`](@ref) can later be resumed, at which point it will pick up right where it left off. At first, this may seem similar to a function call. However there are two key differences. First, switching tasks does not use any space, so any number of task switches can occur without consuming the call stack. Second, switching among tasks can occur in any order, unlike function calls, where the called function must finish executing before control returns to the calling function. This kind of control flow can make it much easier to solve certain problems. In some problems, the various pieces of required work are not naturally related by function calls; there is no obvious "caller" or "callee" among the jobs that need to be done. An example is the producer-consumer problem, where one complex procedure is generating values and another complex procedure is consuming them. The consumer cannot simply call a producer function to get a value, because the producer may have more values to generate and so might not yet be ready to return. With tasks, the producer and consumer can both run as long as they need to, passing values back and forth as necessary. Julia provides a [`Channel`](@ref) mechanism for solving this problem. A [`Channel`](@ref) is a waitable first-in first-out queue which can have multiple tasks reading from and writing to it. Let's define a producer task, which produces values via the [`put!`](@ref) call. To consume values, we need to schedule the producer to run in a new task. A special [`Channel`](@ref) constructor which accepts a 1-arg function as an argument can be used to run a task bound to a channel. We can then [`take!()`](@ref) values repeatedly from the channel object: ```jldoctest producer julia> function producer(c::Channel) put!(c, "start") for n=1:4 put!(c, 2n) end put!(c, "stop") end; julia> chnl = Channel(producer); julia> take!(chnl) "start" julia> take!(chnl) 2 julia> take!(chnl) 4 julia> take!(chnl) 6 julia> take!(chnl) 8 julia> take!(chnl) "stop" ``` One way to think of this behavior is that `producer` was able to return multiple times. Between calls to [`put!()`](@ref), the producer's execution is suspended and the consumer has control. The returned [`Channel`](@ref) can be used as an iterable object in a `for` loop, in which case the loop variable takes on all the produced values. The loop is terminated when the channel is closed. ```jldoctest producer julia> for x in Channel(producer) println(x) end start 2 4 6 8 stop ``` Note that we did not have to explicitly close the channel in the producer. This is because the act of binding a [`Channel`](@ref) to a [`Task()`](@ref) associates the open lifetime of a channel with that of the bound task. The channel object is closed automatically when the task terminates. Multiple channels can be bound to a task, and vice-versa. While the [`Task()`](@ref) constructor expects a 0-argument function, the [`Channel()`](@ref) method which creates a channel bound task expects a function that accepts a single argument of type [`Channel`](@ref). A common pattern is for the producer to be parameterized, in which case a partial function application is needed to create a 0 or 1 argument [anonymous function](@ref man-anonymous-functions). For [`Task()`](@ref) objects this can be done either directly or by use of a convenience macro: ```julia function mytask(myarg) ... end taskHdl = Task(() -> mytask(7)) # or, equivalently taskHdl = @task mytask(7) ``` To orchestrate more advanced work distribution patterns, [`bind()`](@ref) and [`schedule()`](@ref) can be used in conjunction with [`Task()`](@ref) and [`Channel()`](@ref) constructors to explicitly link a set of channels with a set of producer/consumer tasks. Note that currently Julia tasks are not scheduled to run on separate CPU cores. True kernel threads are discussed under the topic of [Parallel Computing](@ref). ### Core task operations Let us explore the low level construct [`yieldto()`](@ref) to underestand how task switching works. `yieldto(task,value)` suspends the current task, switches to the specified `task`, and causes that task's last [`yieldto()`](@ref) call to return the specified `value`. Notice that [`yieldto()`](@ref) is the only operation required to use task-style control flow; instead of calling and returning we are always just switching to a different task. This is why this feature is also called "symmetric coroutines"; each task is switched to and from using the same mechanism. [`yieldto()`](@ref) is powerful, but most uses of tasks do not invoke it directly. Consider why this might be. If you switch away from the current task, you will probably want to switch back to it at some point, but knowing when to switch back, and knowing which task has the responsibility of switching back, can require considerable coordination. For example, [`put!()`](@ref) and [`take!()`](@ref) are blocking operations, which, when used in the context of channels maintain state to remember who the consumers are. Not needing to manually keep track of the consuming task is what makes [`put!()`](@ref) easier to use than the low-level [`yieldto()`](@ref). In addition to [`yieldto()`](@ref), a few other basic functions are needed to use tasks effectively. * [`current_task()`](@ref) gets a reference to the currently-running task. * [`istaskdone()`](@ref) queries whether a task has exited. * [`istaskstarted()`](@ref) queries whether a task has run yet. * [`task_local_storage()`](@ref) manipulates a key-value store specific to the current task. ### Tasks and events Most task switches occur as a result of waiting for events such as I/O requests, and are performed by a scheduler included in the standard library. The scheduler maintains a queue of runnable tasks, and executes an event loop that restarts tasks based on external events such as message arrival. The basic function for waiting for an event is [`wait()`](@ref). Several objects implement [`wait()`](@ref); for example, given a `Process` object, [`wait()`](@ref) will wait for it to exit. [`wait()`](@ref) is often implicit; for example, a [`wait()`](@ref) can happen inside a call to [`read()`](@ref) to wait for data to be available. In all of these cases, [`wait()`](@ref) ultimately operates on a [`Condition`](@ref) object, which is in charge of queueing and restarting tasks. When a task calls [`wait()`](@ref) on a [`Condition`](@ref), the task is marked as non-runnable, added to the condition's queue, and switches to the scheduler. The scheduler will then pick another task to run, or block waiting for external events. If all goes well, eventually an event handler will call [`notify()`](@ref) on the condition, which causes tasks waiting for that condition to become runnable again. A task created explicitly by calling [`Task`](@ref) is initially not known to the scheduler. This allows you to manage tasks manually using [`yieldto()`](@ref) if you wish. However, when such a task waits for an event, it still gets restarted automatically when the event happens, as you would expect. It is also possible to make the scheduler run a task whenever it can, without necessarily waiting for any events. This is done by calling [`schedule()`](@ref), or using the [`@schedule`](@ref) or [`@async`](@ref) macros (see [Parallel Computing](@ref) for more details). ### Task states Tasks have a `state` field that describes their execution status. A [`Task`](@ref) `state` is one of the following symbols: | Symbol | Meaning | |:----------- |:-------------------------------------------------- | | `:runnable` | Currently running, or available to be switched to | | `:waiting` | Blocked waiting for a specific event | | `:queued` | In the scheduler's run queue about to be restarted | | `:done` | Successfully finished executing | | `:failed` | Finished with an uncaught exception |

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully