Skip to content

Instantly share code, notes, and snippets.

@rdp
Created November 11, 2019 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/31632b22d99d9e5c2cc815c3d51ca446 to your computer and use it in GitHub Desktop.
Save rdp/31632b22d99d9e5c2cc815c3d51ca446 to your computer and use it in GitHub Desktop.
require "socket"
lib C
# In C: double cos(double x)
fun sleep(value : UInt32) : UInt32
end
spawn {
loop {
TCPSocket.new("facebook.com",80,0.1,0.1).close
STDOUT.print "."
}
}
loop {
puts "sleeping"
Fiber.yield
C.sleep(1) # or replace with sleep(1)
}
$ crystal bad.cr
sleeping
sleeping
sleeping
Unhandled exception in spawn: connect timed out (IO::Timeout)
from /usr/share/crystal/src/socket/tcp_socket.cr:75:15 in 'initialize'
from /usr/share/crystal/src/socket/tcp_socket.cr:27:3 in 'new'
from bad.cr:9:5 in '->'
from /usr/share/crystal/src/fiber.cr:255:3 in 'run'
from /usr/share/crystal/src/fiber.cr:48:34 in '->'
from ???
sleeping
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment