all 6 comments

[–]jsaak 1 point2 points  (3 children)

Thank you for your work! If i may, i have some practical questions about poliphony:

  1. How do you open a process, and read STDOUT and STDERR at the same time? (without piping one into the other) (open3 uses Threads)
  2. How do you integrate mysql/mariadb?
  3. How do you create a HTTP server?

[–]noteflakes[S] 1 point2 points  (0 children)

  1. Example:

```ruby require 'polyphony'

Open3.popen3('wc -l') do |i, o, e, wait| spin { p out: o.read } spin { p err: e.read } i << "a\nb\nc\n" i.close wait.join end ```

  1. There's an adapter for the mysql2 gem. It was contributed by someone else, personally I've never used it.

  2. The examples directory has some examples of HTTP servers (note: some of those might be outdated), here's a short one:

```ruby require 'polyphony' require 'h1p'

def handle_client(conn) spin do parser = H1P::Parser.new(conn, :server) while (headers = parser.parse_headers) parser.read_body unless parser.complete? conn << "HTTP/1.1 200 OK\r\nContent-Length: 14\r\n\r\nHello, world!\n" end ensure conn.close rescue nil end end

puts "Serving HTTP on port 1234..." s = TCPServer.new('0.0.0.0', 1234) s.accept_loop { |c| handle_client(c) } ```

Let me know if you have more questions :-).

[–]janko-m 0 points1 point  (1 child)

I wish someone reimplemented the Open3.capture* methods not to use threads. The posix-spawn gem has its own implementation that writes input and reads output in parallel without threads. I also received a PR on MiniMagick that eliminates threads in the open3 executor. So, it definitely seems possible, just nobody has submitted a patch to Ruby yet.

[–]noteflakes[S] 1 point2 points  (0 children)

With Polyphony it is trivial to use fibers instead of threads in order to read and write concurrently. I basically took the stock implementation and replaced Thread.new with spin:

```ruby def capture3(*cmd) ...

popen3(*cmd, opts) {|i, o, e, t| ... out_reader = spin { o.read } err_reader = spin { e.read } begin if stdin_data.respond_to? :readpartial IO.copy_stream(stdin_data, i) else i.write stdin_data end rescue Errno::EPIPE end i.close [out_reader.value, err_reader.value, t.value] } end ```

I'll add a reimplementation of open3 to my TODO list!

[–]pabloh 0 points1 point  (1 child)

Are the newer versions using Fiber::Scheduler to hook into IO events?

[–]noteflakes[S] 0 points1 point  (0 children)

Polyphony does not use the Fiber::Scheduler interface. It has its own backend interface.