Please introduce a multi-processing library in Perl or Ruby

后端 未结 5 736
囚心锁ツ
囚心锁ツ 2021-02-04 20:42

In python we can use multiprocessing modules. If there is a similar library in Perl and Ruby, would you teach it? I would appreciate it if you can include a brief sample.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 21:00

    Check out Coro which provide coroutines to Perl.

    Here is an excerpt from the authors docs....

    This module collection manages continuations in general, most often in the form of cooperative threads (also called coros, or simply "coro" in the documentation). They are similar to kernel threads but don't (in general) run in parallel at the same time even on SMP machines. The specific flavor of thread offered by this module also guarantees you that it will not switch between threads unless necessary, at easily-identified points in your program, so locking and parallel access are rarely an issue, making thread programming much safer and easier than using other thread models.

    Unlike the so-called "Perl threads" (which are not actually real threads but only the windows process emulation ported to unix, and as such act as processes), Coro provides a full shared address space, which makes communication between threads very easy. And Coro's threads are fast, too: disabling the Windows process emulation code in your perl and using Coro can easily result in a two to four times speed increase for your programs. A parallel matrix multiplication benchmark runs over 300 times faster on a single core than perl's pseudo-threads on a quad core using all four cores.


    For something similar to above in Ruby then have a look at Fiber which comes with Ruby 1.9.

    Here are two interesting articles on using Fiber:

    • fibers cooperative scheduling in ruby
    • pipelines using fibers

    There is also a Fiber for Perl using Coro. Here are some articles about Fiber for Perl (in Japanese):

    • blog post on writing Fiber for Perl using Coro
    • Fiber for Perl slides

提交回复
热议问题