有没有办法保护一段关键代码? [英] Is there a way to protect a piece of critical code?

查看:60
本文介绍了有没有办法保护一段关键代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想将以下内容作为一个原子操作:


1)将项目附加到列表中

2)设置一个布尔指示符


这就像获取并保持GIL一样,

来防止线程换出这两个操作之间的比较。

- 类似于GIL

似乎使用的倒置函数,看起来像是放开,获得控制权/>
通过阻止I / O返回,然后重新获得


这是反转吗?可能的使用方法?

有没有办法阻止线程交换?


问题出现在多线程的背景下

将列表用作单个生产者的环境,

单个消费者队列 - 我可以用各种方式解决我的问题,其中这是一个,我很好奇至于是否

可以防止线程内部的线程交换。


- 亨德里克

Hi,

I would like to do the following as one atomic operation:

1) Append an item to a list
2) Set a Boolean indicator

It would be almost like getting and holding the GIL,
to prevent a thread swap out between the two operations.
- sort of the inverted function than for which the GIL
seems to be used, which looks like "let go", get control
back via return from blocking I/O, and then "re - acquire"

Is this "reversed" usage possible?
Is there some way to prevent thread swapping?

The question arises in the context of a multi threaded
environment where the list is used as a single producer,
single consumer queue - I can solve my problem in various
ways, of which this is one, and I am curious as to if it is
possible to prevent a thread swap from inside the thread.

- Hendrik

推荐答案

Hendrik van Rooyen写道:
Hendrik van Rooyen wrote:




我会喜欢将以下内容作为一个原子操作:


1)将项目附加到列表中

2)设置布尔指示符


这就像获取并保持GIL一样,

以防止两个操作之间的线程交换。

- 反转函数的类型似乎使用了GIL

,这看起来很像e放开,通过阻止I / O返回获得控制权,然后重新获得


这是逆转"可能的使用方法?

有没有办法阻止线程交换?


问题出现在多线程的背景下

将列表用作单个生产者的环境,

单个消费者队列 - 我可以用各种方式解决我的问题,其中这是一个,我很好奇至于是否可以防止线程内部的线程交换。

Hi,

I would like to do the following as one atomic operation:

1) Append an item to a list
2) Set a Boolean indicator

It would be almost like getting and holding the GIL,
to prevent a thread swap out between the two operations.
- sort of the inverted function than for which the GIL
seems to be used, which looks like "let go", get control
back via return from blocking I/O, and then "re - acquire"

Is this "reversed" usage possible?
Is there some way to prevent thread swapping?

The question arises in the context of a multi threaded
environment where the list is used as a single producer,
single consumer queue - I can solve my problem in various
ways, of which this is one, and I am curious as to if it is
possible to prevent a thread swap from inside the thread.



已经讨论过使用python代码提供GIL。但是

它被认为是一个实现细节 - 这就是你不能按照你想要的方式做你想要的事情的原因。

只需使用常规锁定 - 最后,无论如何都是GIL。


除此之外,你不会得到任何你的方式,因为

线程调度本身不受python控制 - 而是使用OS

线程实现。

Diez

There have been discussions to make the GIL available from python code. But
it is considered a implementation detail - and this is the reason you can''t
do what you need the way you want to.

Just use a regular lock - in the end, that is what the GIL is anyway.

And besides that, you don''t "get" anything by your way, as the
thread-scheduling itself isn''t controlled by python - instead the OS
threading implementation is used.

Diez


Hendrik van Rooyen写道:
Hendrik van Rooyen wrote:




我想将以下内容作为一个原子操作:


1)将项目附加到列表中

2)设置一个布尔指标
Hi,

I would like to do the following as one atomic operation:

1) Append an item to a list
2) Set a Boolean indicator



如果在插入顺序上没有更复杂的条件,我怀疑你在这么简单的单一队列中一定要担心这一点。

该指标应该告诉我什么?那里有一个新元素?


列表本身告诉它的长度,保证增加_after_ .append()

你可以.pop (0)就这样 - 至少在Key / IndexError上捕获/重新启动。


列表.append()和.pop()在任何Python中都是原子的,虽然没有明确提到 - 否则就是离开Python的时候了。


还有Queue.Queue - 尽管它在大多数情况下都有不必要的开销。

阻止Python的函数这种VHL语言中的解释器线程切换对于减少在某些情况下扩展锁的需要是很好的(巨大的代码 - 很少的关键部分)。然而,你的例子到目前为止还不是触发器。我也要求过一次。在非C-Pythons中实现可能很困难。

通常还有一些技术可用于对关键部分进行乐观的无保护执行 - 基本上使用原子计数器,您需要提供用于展开半执行的代码。搜索Google。

Robert


I doubt you have to worry at all about this in such simple single-single queue - if there is not a much more complex condition upon the insert order.
And what should the indicator tell? that a new element is there?

The list itself tells its the length, its guaranteed to be increased _after_ .append()
And you can .pop(0) just so - catching/retring at Key/IndexError at least.

List .append() and .pop() will be atomic in any Python though its not mentioned explicitely - otherwise it would be time to leave Python.

There is also Queue.Queue - though it has unneccessary overhead for most purposes.
A function to block Python interpreter thread switching in such VHL language would be nice for reducing the need for spreading locks in some cases (huge code - little critical sections). Yet your example is by far not a trigger for this. I also requested that once. Implementation in non-C-Pythons may be difficult.
Generally there is also technique for optimistic unprotected execution of critical sections - basically using an atomic counter and you need to provide code for unrolling half executions. Search Google.
Robert


这就像获取并持有GIL一样,

来阻止线程在两个操作之间交换。

- 比使用GIL

似乎使用的倒置函数,看起来像放手,得到控制

通过阻止I / O返回,然后重新获得


这是反转吗?可能的使用方法?

有没有办法阻止线程交换?


问题出现在多线程的背景下

将列表用作单个生产者的环境,

单个消费者队列 - 我可以用各种方式解决我的问题,其中这是一个,我很好奇至于是否

可以防止线程内部的线程交换。


- Hendrik
It would be almost like getting and holding the GIL,
to prevent a thread swap out between the two operations.
- sort of the inverted function than for which the GIL
seems to be used, which looks like "let go", get control
back via return from blocking I/O, and then "re - acquire"

Is this "reversed" usage possible?
Is there some way to prevent thread swapping?

The question arises in the context of a multi threaded
environment where the list is used as a single producer,
single consumer queue - I can solve my problem in various
ways, of which this is one, and I am curious as to if it is
possible to prevent a thread swap from inside the thread.

- Hendrik


" Hendrik van Rooyen" < ma ** @microcorp.co.zawrites:
"Hendrik van Rooyen" <ma**@microcorp.co.zawrites:

我想将以下内容作为一个原子操作:


1)将一个项目附加到列表中

2)设置一个布尔指示符
I would like to do the following as one atomic operation:

1) Append an item to a list
2) Set a Boolean indicator



你可以用其他人建议的锁来做到这一点,但也许你

真的想要Queue模块。

You could do it with locks as others have suggested, but maybe you
really want the Queue module.


这篇关于有没有办法保护一段关键代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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