Skip to content

Commit

Permalink
concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 2, 2021
1 parent 05ea57c commit 9660a30
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crystal/concurrency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Concurrency
{id: concurrency}

## Send and receive
{id: send-and-receive}
{i: Channel}
{i: send}
{i: receive}
{i: spawn}

![](examples/concurrency/send_receive.cr)

## Concurrent HTTP request
{id: concurrent-http-request}

![](examples/concurrency/http_request.cr)

1 change: 1 addition & 0 deletions crystal/crystal.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"time.md",
"http_client.md",
"process.md",
"concurrency.md",
"other.md"
]
}
15 changes: 15 additions & 0 deletions crystal/examples/concurrency/http_request.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "http/client"
puts "before"
ch = Channel(HTTP::Client::Response).new

puts "before spawn"
spawn do
puts "in spawn before send"
res = HTTP::Client.get "https://code-maven.com/"
ch.send res
puts "in spawn after send"
end

puts "before receive"
res = ch.receive
puts "received #{res.body.size} bytes including this row: #{res.body.lines.select(/<title>/)}"
13 changes: 13 additions & 0 deletions crystal/examples/concurrency/send_receive.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
puts "before"
ch = Channel(Int32).new

puts "before spawn"
spawn do
puts "in spawn before send"
ch.send 42
puts "in spawn after send"
end

puts "before receive"
res = ch.receive
puts "received #{res}"

0 comments on commit 9660a30

Please sign in to comment.