且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

建议生成器添加到线程模块。

更新时间:2023-09-16 14:50:16

Andrae Muys写道:
我考虑过为itertools建议它,但实际上它是特定的线程,所以我建议它用于线程模块。




如果您愿意提供此功能,请向sf.net/projects/python提交

a补丁,包括文档更改,

和测试案件。


问候,

马丁


文章< 79 * ********** ***************@posting.google.com>,

Andrae Muys< am *** @ shortech.com.au>写道:

发现自己需要从多个线程序列化访问共享生成器。拿出以下

def serialise(gen):
lock = threading.Lock()
同时1:
lock.acquire()
尝试:
next = gen.next()
最后:
lock.release()
产生下一个




我不确定这是否足以进入标准库。

通常,我会建议有人想要这个功能另外选择

其他选项对此(例如使用Queue.Queue())。

-

Aahz(aa**@pythoncraft.com)< *> http://www.pythoncraft.com/


答:没有。

问:***帖子好吗?


Andrae Muys写道:
发现自己需要从多个线程对共享生成器进行序列化访问。拿出以下

def serialise(gen):
lock = threading.Lock()
同时1:
lock.acquire()
尝试:
next = gen.next()
最后:
lock.release()
产生下一个




是否有任何理由为什么不在线程之间共享锁?

从这看起来,它不会在不同的线程之间同步任何东西

。我错过了什么吗?


亲切的问候,

Ype


发送电子邮件至xs4all.nl


Found myself needing serialised access to a shared generator from
multiple threads. Came up with the following

def serialise(gen):
lock = threading.Lock()
while 1:
lock.acquire()
try:
next = gen.next()
finally:
lock.release()
yield next

I considered suggesting it for itertools, but really it''s thread
specific so I am suggesting it for the threading module.

Andrae Muys

Andrae Muys wrote:
I considered suggesting it for itertools, but really it''s thread
specific so I am suggesting it for the threading module.



If you are willing to contribute this function, please submit
a patch to sf.net/projects/python, including documentation changes,
and test cases.

Regards,
Martin


In article <79**************************@posting.google.com >,
Andrae Muys <am***@shortech.com.au> wrote:

Found myself needing serialised access to a shared generator from
multiple threads. Came up with the following

def serialise(gen):
lock = threading.Lock()
while 1:
lock.acquire()
try:
next = gen.next()
finally:
lock.release()
yield next



I''m not sure this is generic enough to go in the standard library.
Usually, I''d recommend that someone wanting this functionality consider
other options in addition to this (such as using Queue.Queue()).
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?


Andrae Muys wrote:
Found myself needing serialised access to a shared generator from
multiple threads. Came up with the following

def serialise(gen):
lock = threading.Lock()
while 1:
lock.acquire()
try:
next = gen.next()
finally:
lock.release()
yield next



Is there any reason why the lock is not shared among threads?
From the looks of this, it doesn''t synchronize anything
between different threads. Am I missing something?

Kind regards,
Ype

email at xs4all.nl