Non-blocking / Asynchronous Execution in Perl

后端 未结 6 1936
刺人心
刺人心 2021-02-13 13:09

Is there a way to implement non-blocking / asynchronous execution (without fork()\'ing) in Perl?

I used to be a Python developer for many years... Python has really great

6条回答
  •  醉梦人生
    2021-02-13 13:37

    I'm not very familiar with Twisted or POE, but basic parallel execution is pretty simple with threads. Interpreters are generally not compiled with threading support, so you would need to check for that. The forks package is a drop-in replacement for threading (implements the full API) but using processes seamlessly. Then you can do stuff like this:

    my $thread = async {
        print "you can pass a block of code as an arg unlike Python :p";
        return some_func();
    };
    my $result = $thread->join();
    

    I've definitely implemented callbacks from an event loop in an async process using forks and I don't see why it wouldn't work with threads.

提交回复
热议问题