如何在IronPython中自动增加静态成员? [英] How to atomically increment a static member in IronPython?

查看:74
本文介绍了如何在IronPython中自动增加静态成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IronPython脚本使用TPL和Parallel.ForEach使用多个线程处理文件.在C#中,我可以使用Interlocked.Add和Interlocked.Increment在原子线程安全操作中更改全局变量,但这在IronPython中不起作用,因为

I have an IronPython script that uses the TPL and Parallel.ForEach to process files using multiple threads. In C# I can use Interlocked.Add and Interlocked.Increment to change global variables in an atomic thread-safe operation, but this does not work in IronPython because integers are immutable. I currently have a simple Results class that stores a few variables as static members that are used to keep track of the results from a multi-threaded operation. When changing multiple values I can lock the class using the .NET Monitor class to ensure that the update is thread-safe, but this seems like a lot of overhead if I only want to update a single variable (like just increment Results.Files).

我的问题是,是否有更好的方法来增加单个静态成员变量(如Results.IronPython中的文件)的安全性或原子性,类似于Interlocked.Increment的工作方式?另外,是否可以在python或.NET框架中内置任何线程安全计数器来代替基本整数?

My question is if there is a better way to increment a single static member variable like Results.Files in IronPython in a thread-safe or atomic way similar to how Interlocked.Increment works? Alternatively are there any thread-safe counters built into python or the .NET framework that could be used instead of a basic integer?

class Results:
    Files = 0
    Lines = 0
    Tolkens = 0 

    @staticmethod
    def Add(intFiles, intLines, intTolkens): 
        #Use the System.Threading.Monitor class to ensure addition is thread safe
        Monitor.Enter(Results) 
        Results.Files += intFiles
        Results.Lines += intLines
        Results.Tolkens += intTolkens
        Monitor.Exit(Results) #Finish thread safe code

推荐答案

看起来像 python 的方法是使用 multiprocessing.Value 对象,默认情况下,只要访问该对象,它就会锁定该对象.遗憾的是,多处理类不是基于IronType内置于IronPython中的.但是,我确实找到了一种使用Interlocked类和对CLR对象的引用的方法:

Well it looks like the python way to do this would be to use a multiprocessing.Value object, which by default will lock the object whenever it is accessed. Saddly the multiprocessing class is not built into IronPython since it is based on CTypes. I did however find a way to do it using the Interlocked class and a reference to a CLR object:

import clr
from System.Threading import Interlocked
refInt = clr.Reference<int>(5) #Create a reference to an integer
#refInt = <System.Int32 object at 0x0000000000000049 [5]>
#refInt.Value = 5
Interlocked.Increment(refInt) #Returns 6 and refInt now points to a new integer
#refInt = <System.Int32 object at 0x000000000000004A [6]>
#refInt.Value = 6

在这种情况下,您可以使用所有Interlocked方法添加,比较,交换,递增和读取refInt对象.您也可以直接获取或设置refInt.Value,但是只有Interlocked方法才是线程安全的.此外,互锁方法不会引发溢出异常 (它只会以无声方式包装),因此请确保您选择的数据类型足够大,永不溢出.

In that case you can use all of the Interlocked methods to add, compare, exchange, increment, and read the refInt object. You can also get or set the refInt.Value directly, but only the Interlocked methods will be thread-safe. Also the Interlocked methods will NOT throw an overflow exception (it will just wrap silently) so make sure you choose a data type that is large enough to never overflow.

这篇关于如何在IronPython中自动增加静态成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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