从线程返回一个值 [英] returning a value from a thread

查看:48
本文介绍了从线程返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

module1调用module2中的一个函数


module2启动一个调用module3中函数的线程

然后返回module1


线程完成,我需要线程

的返回值,以便在程序流程正在继续的module1中使用。


错误,我希望我解释得那么好吗?


换句话说需要从module3传递的价值

回到module1(全局变量)


我以为有某种记忆类型的容器

我可以用...


ie。从module3在内存中存储值,并且能够从module1读取



我能想到的最接近的是传递队列引用

和队列中module3的stor值然后在module1中读取它们




也许我错过了什么?

解决方案

2004年7月14日星期三,Ken Godee写道:

我以为有某种记忆类型的容器
我可以用...

即。从module3将值存储在内存中,并且能够从module1读取它。

我能想到的最接近的是传递一个队列引用
并将来自module3的值存储在排队,然后在module1中回读它们。




实际上,任何类型的容器对象都可以:你可以传递一个列表,

字典,或线程的类,线程可以存储其结果




一种hackish方法是通过locals()返回的字典

module1到线程。这样线程就可以直接在module1

中设置一个值。


在访问之前,不要忘记在module1中的线程上使用.join()

容器对象! (假设你正在使用线程)我只希望Python的'

..join()可以返回值,就像pthread_join一样。


Christopher T King< sq ****** @ WPI.EDU>写道:

在访问
容器对象之前,不要忘记在module1中的线程上使用.join()! (假设你正在使用线程)我只是希望Python的
.join()可以返回值,比如pthread_join可以。




虽然如果你可以加入线程,然后你必须有一个

引用线程对象,此时你可以做任何你想要允许状态的b $ b b。对该对象进行询问

(直接属性访问,getter等等),这甚至比

加入返回对象更好。


- David


Christopher T King写道:

2004年7月14日星期三,Ken Godee写道:

我以为有某种记忆类型的容器
我可以用...

即。从module3将值存储在内存中,并且能够从module1读取它。

我能想到的最接近的是传递一个队列引用
并将来自module3的值存储在队列,然后在module1中回读它们。



实际上,任何类型的容器对象都可以:你可以传递一个列表,
字典,或线程的类,线程可以将其结果存储在其中。

一个hackish方法是传递
module1中locals()返回的字典到线程。这样线程就可以直接在module1中设置一个值。

在访问
容器对象之前,不要忘记在module1中的线程上使用.join()! (假设你正在使用线程)我只希望Python的
.join()可以返回值,比如pthread_join可以。




如果工作线程执行相对较短的任务

然后在*访问结果之前死亡*,这可能有效。但是列表和词典

不是线程安全的 - 如果它们可能同时被多个

线程访问,那么这种行为将是不可预测的。 (想想

的情况,线程B开始更新字典,插入一个键

但是在它可以将值附加到该键之前被中断,然后
它正在寻找的密钥,但没有有效的引用作为其值...

[免责声明:我不太了解字典的内部运作情况

知道这种确切的情况是否可行,但我确实知道这些字眼

不是线程安全的,所以*类似的*是可能的......])

除非你确定你的工作线程在你获得
之前就死了
结果,你可以让你的主线程可能坐在那里

什么都不做,直到发生这种情况(这就是join()的作用),你需要

使用线程安全的方法来回传递数据。最简单的

这样的方法确实是使用队列。您*可以*使用其他一些

容器,并通过使用

锁来保护对该容器的访问....但这正是队列所做的,为什么要重新发明轮子?


Jeff Shannon

技术员/程序员

Credit International


module1 calls a function in module2

module2 starts a thread that calls a function in module3
and then returns to module1

thread finishes and I need the return value from the thread
to use in module1 where program flow is continuing.

err, I hope I explained that well enough?

In other words need value from module3 passed
back to module1 (global variable)

I thought there was some sort of memory type container
I could use...

ie. store value in memory from module3 and be able to
read it from module1.

The closest I can think of is to pass a queue reference
and stor values from module3 in the queue and then read them
back while in module1.

Maybe I''m missing something?

解决方案

On Wed, 14 Jul 2004, Ken Godee wrote:

I thought there was some sort of memory type container
I could use...

ie. store value in memory from module3 and be able to
read it from module1.

The closest I can think of is to pass a queue reference
and stor values from module3 in the queue and then read them
back while in module1.



Actually, any kind of container object would do: you could pass a list,
dictionary, or class to the thread, and the thread could store its results
in it.

A hackish method would be to pass the dictionary returned by locals() in
module1 to the thread. This way the thread could set a value in module1
directly.

Just don''t forget to .join() on the thread in module1 before accessing the
container object! (Assuming you''re using threading) I just wish Python''s
..join() could return values, like pthread_join can.


Christopher T King <sq******@WPI.EDU> writes:

Just don''t forget to .join() on the thread in module1 before accessing the
container object! (Assuming you''re using threading) I just wish Python''s
.join() could return values, like pthread_join can.



Although if you can join on the thread, then you have to have a
reference to the thread object, at which point you can do anything you
want in terms of permitting state to be interrogated on that object
(direct attribute access, getters, etc...), which is even better than
having join return an object.

-- David


Christopher T King wrote:

On Wed, 14 Jul 2004, Ken Godee wrote:

I thought there was some sort of memory type container
I could use...

ie. store value in memory from module3 and be able to
read it from module1.

The closest I can think of is to pass a queue reference
and stor values from module3 in the queue and then read them
back while in module1.



Actually, any kind of container object would do: you could pass a list,
dictionary, or class to the thread, and the thread could store its results
in it.

A hackish method would be to pass the dictionary returned by locals() in
module1 to the thread. This way the thread could set a value in module1
directly.

Just don''t forget to .join() on the thread in module1 before accessing the
container object! (Assuming you''re using threading) I just wish Python''s
.join() could return values, like pthread_join can.



This may work if the worker thread will perform a relatively short task
and then die *before* you access the result. But lists and dictionaries
are not thread-safe -- if they are potentially accessed by multiple
threads concurrently, then the behavior will be unpredictable. (Think
of a case where thread B starts to update a dictionary, inserts a key
but is interrupted before it can attach a value to that key, and then
while thread B is interrupted thread A looks at the dictionary and finds
the key it''s looking for, but with no valid reference as its value...
[Disclaimer: I don''t know the inner workings of dictionaries well enough
to know if this exact situation is possible, but I do know that dicts
are not threadsafe, so something *similar* is possible...])

Unless you''re certain that your worker thread can die before you get the
results, and you''re able to have your main thread potentially sit around
doing nothing until that happens (which is what join() does), you need
to use a threadsafe method of passing data back and forth. The simplest
such method is indeed to use a queue. You *can* use some other
container, and protect access to that container through the use of
locks.... but that''s exactly what a queue does, so why reinvent the wheel?

Jeff Shannon
Technician/Programmer
Credit International


这篇关于从线程返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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