Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:44:10+00:00 2026-05-13T18:44:10+00:00

I have an IronPython script that uses the TPL and Parallel.ForEach to process files

  • 0

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).

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:44:10+00:00Added an answer on May 13, 2026 at 6:44 pm

    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
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an IronPython script that looks for current running processes using WMI. The
I have an ironpython app that uses dlls. Some of the dlls written in
I have an embedded scripting engine in my C# application that uses IronPython 2.
I have written an IronPython script that generates a xml file that contains the
I have a Iron Python script that I want to run and then have
I have seen documentation on the IronPython mailing list that describes how to write
The majority of material I have found regarding DLR is related to IronPython. Can
Working on a project that uses some IronPython scripts to as plug-ins, that utilize
I'd like to use embedded IronPython as a way of allowing users to script
Can someone recommend a workaround for this ironpython bug? I have a class contained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.