VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/lib/gems/2.5.0/gems/em-synchrony-1.0.3/examples/go/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/lib/gems/2.5.0/gems/em-synchrony-1.0.3/examples/go/README.md
# CSP Experiments with Ruby

Partly an exercise to help myself wrap my head around Go's concurrency, partly an experiment to see how much of the syntax & behavior of Go's CSP model can be modelled in Ruby... As it turns out, it's not hard to almost replicate the look and feel.

Note: none of the Ruby examples actually give you the parallelism of Go.

## Notes
 * Instead of explicitly using locks to mediate access to shared data, Go encourages the use of channels to pass references to data between goroutines.

 * Channels combine communication — the exchange of a value—with synchronization — guaranteeing that two calculations (goroutines) are in a known state.

go.rb implements an (un)bounded Channel interface, and with some help from Fibers & Ruby 1.9, we can also implement the goroutine look and feel pretty easily. In fact, with CSP semantics, its not hard to imagine a MVM (multi-VM) Ruby where each VM still has a GIL, but where data sharing is done via communication of references between VM's.

## Simple channel example in Go

    package main

    import (
      "fmt"
      "time"
    )

    func main() {
      c := make(chan string)

      go func() {
        time.Sleep(1)
        c <- "go go go sleep 1!"
       }()

       fmt.Printf("%v\n", <-c)  // Wait for goroutine to finish
    }

## Equivalent in Ruby

    require 'go'

    EM.synchrony do
      c = Channel.new

      go {
        sleep(1)
        c << 'go go go sleep 1!'
      }

      puts c.pop

      EM.stop
    end


VaKeR 2022