Python Forum
Code running many times nad not just one?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code running many times nad not just one?
#1
hello,
something isn't understaood for me
I have this code :

my_os = platform.system()
print("OS in my system : ", my_os)

if my_os == 'Windows':
    UploadDirectory = r'r"C:\\Test\\FilesToUpload\\'
    DeviceError= r"C:\\Test\\logs\\DeviceError.txt"
    MysqlError_Log = r"C:\\Test\\logs\\MysqlErrorLog.txt"
    MysqlSuccess_Log = r"C:\\Test\\logs\\MysqlSuccessLog.txt"
    DeviceSuccess= r"C:\\Test\\logs\\DeviceSuccess.log"
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        handlers=[RotatingFileHandler(filename=r'C:\\Test\\logs\\GetConst.log', mode='w')])
else:
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        handlers=[RotatingFileHandler(filename='/home/pi/logs/GetConst.log', mode='w')])
    UploadDirectory = r'/home/pi/FilesToUpload/'
    DeviceError= "/home/pi/logs/DeviceError.txt"
    MysqlError_Log = "/home/pi/logs/MysqlErrorLog.txt"
    MysqlSuccess_Log = "/home/pi/logs/MysqlSuccessLog.txt"
    DeviceSuccess= "/home/pi/logs/DeviceSuccess.log"

print('will clear all logs files')
open(DeviceError, 'w').close()
open(MysqlSuccess_Log, 'w').close()
open(MysqlError_Log, 'w').close()
open(DeviceSuccess, 'w').close()

def getListFromMysql():
.
.

def function2():
.
.


if __name__ == '__main__':
    while 1:
        logging.info('Start running now')
        StartTime = datetime.datetime.now()
        print(str(StartTime) + '   Start Time of the program')
        MysqlDataList = getListFromMysql()
        try:
            with Pool(50) as p:
                List1 = p.map(function2, MysqlDataList)
        except Exception as e:
            print('Pool Error')
            print(e)
        EndTime = datetime.datetime.now()
        print('Total : ' + str(len(MysqlDataList)) + ' Devices')
        print(str(EndTime) + '   End Time of the program')
when I run it in Pycharm it run the first part (before the while) many times, why ?
shouldn't it run only once? this part is before the loop \ while


OS in my system :  Windows     1
will clear all logs files
2022-07-17 10:51:23.076256   Start Time of the program
There are 3 Devices!
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files
OS in my system :  Windows     1
will clear all logs files

Process finished with exit code -1
Reply
#2
Not sure if we have all the code, but I will point out - All of the code at the top executes before you hit the dunder name == dunder main. Generally recommended that you put that code into another function and call it after the check. Doesn't really explain why it executes many times, but try putting it in a function. If that does not solve, post the rest of the code.
Reply
#3
maybe the question should be why I'm getting error while running under __name__

if i try to run without it I get

try:
    with Pool(50) as p:
        List1 = p.map(GetDataFromRouterAndUpdateSQL, MysqlDataList)
except Exception as e:
    print('Pool Error')
    print(e)
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
2022-07-17 14:54:33.881169   Start Time of the program
2022-07-17 14:54:33.959169   Start Time of the program
There are 3 routers!
2022-07-17 14:54:34.083969   Start Time of the program
There are 3 routers!
2022-07-17 14:54:34.146369   Start Time of the program
There are 3 routers!
2022-07-17 14:54:34.208769   Start Time of the program
2022-07-17 14:54:34.208769   Start Time of the program
2022-07-17 14:54:34.208769   Start Time of the program
Traceback (most recent call last):
  File "<string>", line 1, in <module>
There are 3 routers!
  File "C:\Users\David\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 116, in spawn_main
There are 3 routers!
There are 3 routers!
There are 3 routers!
Pool Error
Reply
#4
Linux and Mac use "fork()" go make processes. Forked processes start out knowing everything the parent process knows. Windows uses "spawn()" to make processes. Spawned processes start out knowing nothing. To teach the spawned process about the parent process, Python executes the parent module in each of the spawned processes. That is why it looks like the top code is run multiple times, because it is. Once for the parent process and once for each spawned process.

fork() vs spawn() also makes the processes act differently. A forked process knows all about the objects that existed in the parent process when the process is forked. These objects can be used to communicate between the parent and forked process, or for multiple forked processes to communicate with each other. This does not work on Windows. Objects in the parent process are unreachable from the spawned process. You may have variables with the same name, but they are different objects.

This also explains your second problem. If you are running in Windows you are not using fork().
Reply
#5
I guess this is the prolblem

I didn't know it

Thanks ,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 659 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 395 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 533 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Different code execution times Wirbelwind94 4 764 Oct-06-2023, 12:30 PM
Last Post: snippsat
Question Running an action only between certain times alexbca 9 1,733 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Error while running code on VSC maiya 4 3,776 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,514 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Why is this Python code running twice? mcva 5 5,296 Feb-02-2022, 10:21 AM
Last Post: mcva
  Python keeps running the old version of the code quest 2 3,789 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  My python code is running very slow on millions of records shantanu97 7 2,588 Dec-28-2021, 11:02 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020