Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 scripts running from crontab simultaneously reference the same instance of a variable #54162

Closed
yuri mannequin opened this issue Sep 26, 2010 · 2 comments
Closed

2 scripts running from crontab simultaneously reference the same instance of a variable #54162

yuri mannequin opened this issue Sep 26, 2010 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@yuri
Copy link
Mannequin

yuri mannequin commented Sep 26, 2010

BPO 9953

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-09-26.13:55:26.980>
created_at = <Date 2010-09-26.13:52:51.559>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = '2 scripts running from crontab simultaneously reference the same instance of a variable'
updated_at = <Date 2010-09-26.13:55:26.969>
user = 'https://bugs.python.org/yuri'

bugs.python.org fields:

activity = <Date 2010-09-26.13:55:26.969>
actor = 'exarkun'
assignee = 'none'
closed = True
closed_date = <Date 2010-09-26.13:55:26.980>
closer = 'exarkun'
components = ['Interpreter Core']
creation = <Date 2010-09-26.13:52:51.559>
creator = 'yuri'
dependencies = []
files = []
hgrepos = []
issue_num = 9953
keywords = []
message_count = 2.0
messages = ['117416', '117417']
nosy_count = 2.0
nosy_names = ['exarkun', 'yuri']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue9953'
versions = ['Python 3.1']

@yuri
Copy link
Mannequin Author

yuri mannequin commented Sep 26, 2010

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:

  1. gets a logger instance and adds FileHandler,
  2. prints to its log the current time and PID,
  3. prints ID of the logger instance,
  4. prints ID and value of some variable
  5. sleeps for 3 sec,
  6. 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

@yuri yuri mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 26, 2010
@exarkun
Copy link
Mannequin

exarkun mannequin commented Sep 26, 2010

You can't rely on id() to return distinct values across different processes. It guarantees uniqueness *within a single process* (at any particular moment).

In other words, you're misusing id() here. This is not a Python bug.

@exarkun exarkun mannequin closed this as completed Sep 26, 2010
@exarkun exarkun mannequin added the invalid label Sep 26, 2010
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants