建议生成器添加到线程模块。 [英] Suggested generator to add to threading module.

查看:59
本文介绍了建议生成器添加到线程模块。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现自己需要从

多个线程对共享生成器进行序列化访问。得出以下


def serialise(gen):

lock = threading.Lock()

而1:

lock.acquire()

试试:

next = gen.next()

最后:

lock.release()

下一个产量


我考虑过为itertools提出建议,但实际上是它的主题

具体所以我建议使用线程模块。


Andrae Muys

解决方案

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


这篇关于建议生成器添加到线程模块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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