开发者

How do I use async programming in Perl?

Is there a simple way to do async in Perl? I have the following logic for an Apache app:

get requ开发者_JS百科est
process request
write to log
return

what I want to do is to have the "write to log" part to be async, so that I can do the "return" part asap.


Unfortunately, this typically entails the POE framework, but there is also a growing alternative (which I would want to try first) called AnyEvent.

See this question for learning materials for more information about learning POE. POE is a framework and it tends to eat your whole application. It also doesn't look like Perl, and sticks out. I believe it is a big learning curve for your typical application.

AnyEvent is simple continuation based asyncronous tasking, you should be able to figure it out fairly well with just the CPAN docs.

For your specific question you would use AnyEvent's AIO or POE's Read Write wheel


Consider looking at Coro.

From its CPAN documentation:

Unlike the so-called "Perl threads" (which are not actually real threads but only the windows process emulation (see section of same name for more details) 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.

This includes Coro::AIO, a "truly asynchronous file and directory I/O", which might be what you're looking for.


Do you really need to write the logs before returning?

Assuming this is Apache mod_perl, it supports cleanup handlers that get triggered for the child process after the response is returned.

It seems what you REALLY want is for the logging not to stop the return, cleanup handlers would seem to do that without the need for async at all.


you can use threads in perl. create a thread that handle the logs. this thread would have a buffer that the main thread can add messages to it


Many-core Engine for Perl (MCE) comes with several examples demonstrating matrix multiplication in parallel across many cores. The readme contains benchmark results as well.

https://metacpan.org/source/MARIOROY/MCE-1.514/examples/matmult/README

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜