dongshi2458 2015-08-10 18:59
浏览 16
已采纳

在Perl6中有与Go goroutines等效的东西吗?

I can see that

perl6 -e '@r = do for ^500 {start { .say; sleep 3 }}; await @r'

makes about a dozen of moar threads on my system and use them as pool for promises, but I would like to start them all at once like in Go. Is that possible?

  • 写回答

1条回答 默认 最新

  • doulianxi0587 2015-08-11 01:51
    关注

    From what I understand goroutines are a very low level construct.
    Most things in Perl are not very low level.

    The closest to what you think you want is probably to directly use Threads.

    my @r = do for ^100 { # currently aborts if it's much over 100
      Thread.start: { .say; sleep 3 };
    }
    
    # The implementation may actually create several threads
    # for this construct
    @r>>.finish;
    

    I highly recommend you not do that though as it is not very composable.


    If you instead wanted to print out the numbers after waiting 3 seconds I would have used the .in method of Promise which returns a Promise that will be kept in that many seconds.
    This example appears to create 500 threads nearly simultaneously, but may not actually start any additional threads.

    my @r = (^500).map: {
      Promise.in(3).then: -> $ { .say }
    }
    await @r;
    

    Perl 6 also has Supplies and Channels

    # get three events fired each second
    my $supply = Supply.interval: 1/3;
    
    react {
      # not guaranteed to run in order.
      whenever $supply.grep(* %% 3) { print 'Fizz' }
      whenever $supply.grep(* %% 5) { print 'Buzz' }
    
      whenever $supply {
        sleep 1/10; # fudge the timing a little
        .put
      }
    
      whenever Promise.in(10) {
        say 'ten seconds have elapsed';
        done
      }
    }
    

    In general the design of asynchronous constructs are there to hide some of the hairier things you have to handle with threads.

    In fact one of the easiest ways to use threads may well be to just add hyper or race before a map or grep method call:

    my @r = (^50).hyper( :batch(5), :degree(10) ).map: { .say; sleep 1 }
    # use `race` instead of `hyper` if you don't
    # care about the order of @r
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable