Perl 对象线程
大家好,我有一个关于 perl 对象和线程的问题。
我的程序是一个日志解析器,它将日志的每一行处理成一个对象列表,并且 因此,每个对象都可以像这样引用:
获取存储的值:
print @object[$index]->time;
output : 01:11:22
设置值:
@object[$index]->time("01:22:25");
print @object[$index]->time;
output : 01:22:25
将所有日志保存到内存后,我将有@objects 的列表。
所以问题就出在对象的处理上。
我想通过线程处理数据,并且可以对普通数据执行此操作,例如修改字符串、添加总计等。 但我不知道如何对对象执行此操作。
我想做类似的事情
my $thr2 = threads->new(\&processTime, @object);
(示例不正确,只是我想要如何处理的想法)
因此我需要你们的帮助来帮助我通过线程处理对象。谢谢
Hey all, I have a question about perl objects and threading.
My program is a log parser that processes each line of the logs into an object list, and
thus, each of the objects can be referenced like this :
to get the value stored :
print @object[$index]->time;
output : 01:11:22
to set the value :
@object[$index]->time("01:22:25");
print @object[$index]->time;
output : 01:22:25
And after saving all of the logs into memory, I will have a list of @objects.
So the problem lies in the processing of the objects.
I want to process the data via threads and I am able to do it on normal data like modifying a string, adding a total, etc.
But I do not know how to do so for objects.
I want to do something like
my $thr2 = threads->new(\&processTime, @object);
(example is not correct, just an idea of how I want the processing to be)
So therefore I require the help of you guys to help me with the processing of objects via threads. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要的只是并发处理,那么使用分叉而不是使用线程的问题要少得多。一个流行的分叉库是 Parallel::ForkManager,尽管还有其他库。
如果您觉得必须使用线程,请参阅 Thread::Queue 但请注意其建议,请仔细阅读线程::共享错误和限制部分,特别是阅读 共享安全类的示例。
另外,在 perl5 中,数组元素的访问方式为
$object[$index]
,而不是@object[$index]
;后者是一个数组切片,在许多情况下它的工作方式相同,但效率较低。如果您启用了警告(您应该这样做),您应该会看到有关此的警告。If all you want is concurrent processing, there are far fewer problems with forking instead of using threads. One popular forking library is Parallel::ForkManager, though there ar e others.
If you feel you must use threads, see Thread::Queue but note its advice to carefully read the threads::shared bugs and limitations section, and particularly read the example of a share-safe class.
Also, in perl5, an array element is accessed as
$object[$index]
, not@object[$index]
; the latter is an array slice, which will work the same in many circumstances, but is less efficient. If you have warnings enabled (which you should), you should be seeing a warning about this.IIRC perl 对多个线程使用的任何对象 (ref) 执行写时复制,除非该对象被显式标记为共享。阅读threads::shared perldoc 页面。
IIRC perl does copy-on-write on any object (ref) used by multiple threads unless the object is explicitly marked for sharing. Read the threads::shared perldoc page.