有没有办法在 Perl 中管理进程(即实际工作的线程替换)?
我在 perl 中有一个多线程应用程序,我必须依赖几个非线程安全模块,因此我一直在使用带有 kill()
信号的 fork()
进程作为消息传递接口。
问题在于信号处理程序有点不稳定(至少可以这么说),并且通常会导致进程在不适当的状态下被杀死。
有一个更好的方法吗?
I have a multithreded application in perl for which I have to rely on several non-thread safe modules, so I have been using fork()
ed processes with kill()
signals as a message passing interface.
The problem is that the signal handlers are a bit erratic (to say the least) and often end up with processes that get killed in inapropriate states.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您的程序需要执行的具体操作,您可以考虑使用 POE,它是一个 Perl具有用户空间线程的多线程应用程序框架。 它很复杂,但优雅且功能强大,可以通过将活动限制在单个 Perl 解释器线程中来帮助您避免非线程安全模块。
有用的入门资源:
另外还有数百个预构建的POE 组件,您可以用来组装到应用程序中。
Depending on exactly what your program needs to do, you might consider using POE, which is a Perl framework for multi-threaded applications with user-space threads. It's complex, but elegant and powerful and can help you avoid non-thread-safe modules by confining activity to a single Perl interpreter thread.
Helpful resources to get started:
Plus there are hundreds of pre-built POE components you can use to assemble into an application.
您始终可以在父级和子级之间使用管道来来回传递消息。
这不是一种非常舒适的编程方式,但它不应该“不稳定”。
You can always have a pipe between parent and child to pass messages back and forth.
Not a very comfortable way of programming, but it shouldn't be 'erratic'.
看看 forks.pm,一个“drop -使用 fork() 替换 Perl 线程”,这使得内存使用更加合理(但不要在 Win32 上使用它)。 它将允许您声明“共享”变量,然后自动在进程之间传递对此类变量所做的更改(类似于threads.pm 的工作方式)。
Have a look at forks.pm, a "drop-in replacement for Perl threads using fork()" which makes for much more sensible memory usage (but don't use it on Win32). It will allow you to declare "shared" variables and then it automatically passes changes made to such variables between the processes (similar to how threads.pm does things).
从 perl 5.8 开始,您应该查看核心线程模块。 请查看 http://metacpan.org/pod/threads
如果你想使用以下模块, 不是线程安全的,您通常可以使用 require 加载它们并在线程入口点内导入。
From perl 5.8 onwards you should be looking at the core threads module. Have a look at http://metacpan.org/pod/threads
If you want to use modules which aren't thread safe you can usually load them with a require and import inside the thread entry point.