Perl解释器线程有哪些替代方案? [英] What are the alternatives to Perl interpreter threads?

查看:63
本文介绍了Perl解释器线程有哪些替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

perldoc线程说:

正式禁止在perl中使用基于解释器的线程.

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

还有其他基于Perl的线程吗?还是我们不应该在Perl中使用线程?

Are there any other Perl based threads? Or should we just not use threads in Perl?

推荐答案

取决于您要完成的工作.我仍然广泛使用线程,并且它们没有太大的问题.

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.

相反,产生一个线程就像再次启动代码一样,但是带有一些有用的IPC挂钩.这意味着您确实不希望像您所想的那样执行程序的每线程任务模型.

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.

相反,Thread::Queue工作线程样式模型将为您提供更好的服务.这是一个例子: Perl用子守护程序进行守护

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

但是,您可能想考虑使用fork作为替代.由于fork是在Unix上实现的,因此它是非常有效的系统调用,并且对于生成新进程非常有效. 缺点是-做IPC不太友好.

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是我喜欢为多处理进行分叉的一个模块.

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

但是无论哪种情况,您都应该注意-多重处理不是万能的.如果您有正确的问题要解决,它可以让您占用更多的CPU.它不会使您的磁盘运行得更快:)

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 :)

这篇关于Perl解释器线程有哪些替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆