如何调试await multi Promise时会被系统杀死的脚本?

che*_*nyf 6 sockets promise async-await raku

回答 “脚本使用了太多内存”

\n
\n

我开始逐行start读取文本文件,并将文本转换为 blob,然后将其发送到套接字:

\n
    use experimental :pack;\n\n    sub heart-msg(Str $msg, Str $device-id --> Blob) {\n        my $heart-msg = $msg.substr(0, $msg.chars - 8);\n        my $header-buf = pack("H*", $heart-msg);\n        my $device-id-buf = pack("L*", $device-id);\n\n        $header-buf ~ $device-id-buf\n    }\n\n    sub deal-message(Str $msg, Str $device-id --> Blob) {\n        my $now    = DateTime.now();\n        my $year   = $now.year;\n        my $month  = $now.month;\n        my $day    = $now.day;\n        my $hour   = $now.hour;\n        my $minute = $now.minute;\n        my $second = $now.second.Int;\n        my $check-number = 0;\n\n        my $head-msg         = $msg.substr(0,4);\n        my $device-id-length = $msg.substr(4,2);\n        my $pf-code          = $msg.substr(14, 2);\n        my $msg-length       = $msg.substr(16, 4);\n        my $rel-time         = $msg.substr(20, 4);\n        my $end-msg          = $msg.substr(38, $msg.chars - 38 - 2);\n\n        my $head-msg-buf      = pack("H*", $head-msg);\n        my $device-id-len-buf = pack("H*", $device-id-length);\n        my $device-id-buf     = pack("L*", $device-id);\n        my $pf-code-buf       = pack("H*", $pf-code);\n        my $msg-length-buf    = pack("H*", $msg-length);\n        my $rel-time-buf      = pack("H*", $rel-time);\n        my $year-buf          = pack("S*", $year);\n        my $month-buf         = pack("C*", $month);\n        my $day-buf           = pack("C*", $day);\n        my $hour-buf          = pack("C*", $hour);\n        my $minute-buf        = pack("C*", $minute);\n        my $second-buf        = pack("C*", $second);\n        my $end-msg-buf       = pack("H*", $end-msg);\n\n        my @bufs = [$device-id-len-buf, $device-id-buf, $pf-code-buf, $msg-length-buf,\n        $rel-time-buf, $year-buf, $month-buf, $day-buf, $hour-buf, $minute-buf, $second-buf,\n        $end-msg-buf];\n\n        for @bufs -> $byte {\n            my $byte_sum = [+] $byte.contents;\n            $check-number += $byte_sum;\n        }\n\n        $check-number = $check-number % 256;\n        my $checked-msg-buf = pack("C*", $check-number);\n\n        [~] $head-msg-buf, |@bufs, $checked-msg-buf\n    }\n\n\n    sub deal-data(Str $msg, Str $device-id --> Blob) {\n        my $header = $msg.substr(0, 4);\n        given $header {\n            when \'6868\' {\n                return deal-message($msg, $device-id);\n            }\n            when \'7468\' {\n                return heart-msg($msg, $device-id);\n            }\n            default { return Buf.new }\n        }\n    }\n\n\nsub MAIN(\n         :$host = \'10.0.0.77\',\n         Int() :$port = 4444,\n         Rat() :$interval = 0.001,\n         :$file = \'data.txt\',\n         Int() :$device-num = 169) {\n    my $conn = IO::Socket::INET.new(:$host, :$port);\n    my @devices = "modelId.txt".IO.lines;\n\n    my @promises = gather for @devices[159..$device-num] -> $device-id {\n        take start {\n            my $f = lazy $file.IO.lines;\n            my $iterator = $f.iterator;\n\n            react {\n                whenever Supply.interval($interval) {\n                    try {\n                        my $line := $iterator.pull-one;\n\n                        if $line =:= IterationEnd {\n                            done;\n                        } else {\n                            my $blob = deal-data($line.chomp.split(/\\s+/).tail, $device-id.Str);\n                            #say $blob;\n                            $conn.write($blob);\n                        }\n                    }\n\n                    QUIT {\n                        $conn.close;\n                        say \'connecting closed\';\n                        default {\n                            say .^name, \'\xe2\x86\x92 \', .Str;\n                            say "handled in line $?LINE";\n                        }\n                    }\n                    LAST {\n                        say "Connection closed";\n                        done;\n                    }\n                }\n            }\n        }\n    }\n    await @promises;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当在 CentOS 7.4(12 核,32G RAM)上运行时,几秒钟后,我的脚本被系统杀死。在Win11(12核,16G RAM)上运行时,没问题。

\n

那么如何调试这个脚本呢?

\n

在此输入图像描述

\n