This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: lost global variables in main memory intensive execution
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, ironfroggy, jpahullo, sf-robot
Priority: normal Keywords:

Created on 2007-04-25 09:45 by jpahullo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_global.py jpahullo, 2007-04-25 09:45 python code, structure of the problem
Messages (4)
msg31883 - (view) Author: Jordi Pujol Ahulló (jpahullo) Date: 2007-04-25 09:45
Hello,

I was running a very main memory intensive program in my computer. I looked for similar bugs or troubles in the site and I didn't found any similar to this. I know that the use of global variables is not recommended by software engineering, but for special cases it is very fast to develop/test some ideas.

My problem statement is the following (very simplified and also attached):

########## BEGINNING OF CODE
#test for globals

counter_1 = 0

def modifierfunction():
    global counter_1

    #some code
    counter_1 += 1
    #some other code

def test():
    for i in range(2):
        global counter_1
        counter_1 = 0

        for j in range(10):
            modifierfunction()

        print "COUNTER_1:", counter_1

def test2():
    global counter_1
    for i in range(2):
        counter_1 = 0

        for j in range(10):
            modifierfunction()

        print "COUNTER_1:", counter_1

if __name__ == "__main__":
    test()
    test2()
########## END OF CODE

Globally speaking, it is a global variable, defined at the begining of the python file (counter_1), and it is modified in some functions within it (in this example, modifierfunction). At the end, it appear some tests that make what I need. 

If you try to run this code, it will always show the expected values in the standard out. But, let me to show you my problem.

In the beginning, I have the global statement as in test2. But I found that it only take a corrent value for the first iteration. The others it has always a zero value. I didn't understand anything.

Then, some collegue suggested me to change the place of the global statement (as in test()), and then it worked correctly, as it was expected.

I repeat. The above example works fine. But when this same structure, but with my big problem, very main memory intensive, test2() didn't work correctly.

Thank you for your attention.

Regards,

Jordi
msg31884 - (view) Author: Calvin Spealman (ironfroggy) Date: 2007-04-28 14:59
If you really think there is a bug, don't post working code, post code that actually demonstrates the problem. Something anyone else could use to reproduce the bug.
msg31885 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-05-11 11:17
The resulting bytecode the functions are compiled to is exactly the same, no matter where the global statement is.

So I cannot believe that moving the global statement alone causes something that failed to work.
msg31886 - (view) Author: SourceForge Robot (sf-robot) Date: 2007-05-26 02:20
This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).
History
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44896
2007-04-25 09:45:16jpahullocreate