Skip to content

Instantly share code, notes, and snippets.

@nilstoedtmann
Last active May 2, 2020 22:57
Show Gist options
  • Save nilstoedtmann/f983fea5d7c2f915549111b5d970e69f to your computer and use it in GitHub Desktop.
Save nilstoedtmann/f983fea5d7c2f915549111b5d970e69f to your computer and use it in GitHub Desktop.
Test multiple Python processes logging to same file
'''Write 1000 log messages; wait 1-10ms inbetween.'''
# See also:
# - https://stackoverflow.com/q/15096090
# - https://stackoverflow.com/q/38219512
# To test, fork 1000 concurrent processes:
# for i in {1..1000} ; do ( python logtest.py & ) ; done
import logging, random, time, os
pid = os.getpid()
LOGFILE = 'test.log'
logging.basicConfig(
filename=LOGFILE, level=logging.DEBUG,
format='%(asctime)s LogTest [{}] %(message)s'.format(pid),
)
log = logging.getLogger(__name__)
for _ in range(1000):
log.info("FOO BAR!")
time.sleep(random.uniform(0.001, 0.01))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment