Issue 9953: 2 scripts running from crontab simultaneously reference the same instance of a variable (original) (raw)
Originally the problem was that one script used a logger instance initialized in another script, and, as a result, log entries were "signed" by the later one.
Setup: python 3.1.1, Suse Linux enterprise server 9
2 scripts are scheduled in crontab as follows: */1 * * * * /my_path/python/test1.py > /dev/null 2>&1 */1 * * * * /my_path/python/test2.py > /dev/null 2>&1
Each script does:
- gets a logger instance and adds FileHandler,
- prints to its log the current time and PID,
- prints ID of the logger instance,
- prints ID and value of some variable
- sleeps for 3 sec,
- prints the current time again.
Result: each script prints the same IDs of the variables
test1.py
#!/usr/local/bin/python3 import logging from logging import FileHandler from datetime import datetime import time import os import sys log = logging.getLogger('MyLog1') log.setLevel(logging.INFO) logFilePath = os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])), 'log') dh = FileHandler(os.path.join(logFilePath, 'log1')) log.addHandler(dh) someVariable = 9 log.info(str(datetime.now()) + ' PID=' + str(os.getpid())) log.info('logger ID=' + str(id(log))) log.info('someVariable ID=' + str(id(someVariable)) + 'value=' + str(someVariable)) time.sleep(3) log.info(str(datetime.now()) + ' PID=' + str(os.getpid()))
test2.py:
#!/usr/local/bin/python3 import logging from logging import FileHandler from datetime import datetime import time import os import sys log = logging.getLogger('MyLog2') log.setLevel(logging.INFO) logFilePath = os.path.join(os.path.realpath(os.path.dirname(sys.argv[0])), 'log') dh = FileHandler(os.path.join(logFilePath, 'log2')) log.addHandler(dh) someVariable = 10 log.info(str(datetime.now()) + ' PID=' + str(os.getpid())) log.info('logger ID=' + str(id(log))) log.info('someVariable ID=' + str(id(someVariable)) + 'value=' + str(someVariable)) time.sleep(3) log.info(str(datetime.now()) + ' PID=' + str(os.getpid()))
Result:
log1: 2010-09-26 15:45:01.531460 PID=5704 logger ID=182908380624 someVariable ID=7167488value=9 2010-09-26 15:45:04.535591 PID=5704
log2: 2010-09-26 15:45:01.528691 PID=5705 logger ID=182908380624 someVariable ID=7167520value=10 2010-09-26 15:45:04.534598 PID=5705
If I change value of someVariable to 9 in the both scripts, I have:
log1: 2010-09-26 15:48:01.488008 PID=6036 logger ID=182908380624 someVariable ID=7167488value=9 2010-09-26 15:48:04.491977 PID=6036
log2: 2010-09-26 15:48:01.490214 PID=6035 logger ID=182908380624 someVariable ID=7167488value=9 2010-09-26 15:48:04.494991 PID=6035