• [^] # Re: Hello World concurrentiel

    Posté par . En réponse à la dépêche De tout, de rien, des bookmarks, du bla‐bla #45. Évalué à -1. Dernière modification le 09 novembre 2012 à 15:53.

    Ruby (http://ruby-doc.org/stdlib-1.8.7/libdoc/monitor/rdoc/monitor_rb.html) :

    require 'monitor.rb'
    buf = []
    buf.extend(MonitorMixin)
    empty_cond = buf.new_cond
    # consumer
    Thread.start do
     loop do
     buf.synchronize do
     empty_cond.wait_while { buf.empty? }
     print buf.shift
     end
     end
    end
    # producer
    while line = "hello world!\n"
     buf.synchronize do
     buf.push(line)
     empty_cond.signal
     end
    end
    
    

    Output:

    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!
    hello world!

    Usage CPU : mes deux cores à ~66% chacun dans htop :o
    (j'aurais peut-être atteint 100% avec deux processus à l'aide de IO.popen, par exemple ?)

    Bref, qui se dévoue pour Python ?

    "Quand certains râlent contre systemd, d'autres s'attaquent aux vrais problèmes." (merci Sinma !)