What are the alternatives to Perl interpreter threads?

后端 未结 3 913
天涯浪人
天涯浪人 2021-02-20 05:34

perldoc threads says:

The use of interpreter-based threads in perl is officially discouraged.

Are there any other Perl based threads

相关标签:
3条回答
  • 2021-02-20 06:00

    That warning is poppycock. It should be removed. The developers of Perl explained that it means "The use of interpreter-based threads in perl is officially discouraged if you want a lightweight system for multitasking".

    Since creating new threads can be expensive, just use a model that involves reusable worker threads.

    0 讨论(0)
  • 2021-02-20 06:12

    As long as I know, there are not any reliable thread implemantations. You should stick to some event-based modules, like Coro, AnyEvent, IO::Async etc.

    0 讨论(0)
  • 2021-02-20 06:25

    Depends on what you're trying to accomplish. I still use threads extensively, and there isn't a massive problem with them.

    The biggest problem with them is that they are not lightweight, where if you've threaded in other languages, you might expect them to be.

    They're more the opposite - spawning a thread is like starting your code again, but with some useful hooks for IPC. That means you really don't want to be doing a task-per-thread model of program, like you might be thinking of.

    Instead, you would be much better served by a Thread::Queue worker-thread style model. Here's an example of that: Perl daemonize with child daemons

    However, you may want to consider using fork as an alternative. fork - because of it's implementation on Unix - is a very efficient system call, and can be quite efficient for spawning new processes. The downside is - it's not quite as friendly for doing IPC.

    Parallel::ForkManager is one module that I like for doing forking for multiprocessing.

    But in either case you should note - multiprocessing isn't a magic bullet. It lets you hog more CPUs if you have the right sort of problem to solve. It won't make your disks go any faster :)

    0 讨论(0)
提交回复
热议问题