What are the alternatives to Perl interpreter threads?

后端 未结 3 917
天涯浪人
天涯浪人 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: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 :)

提交回复
热议问题