import threading
import logging
import subprocess
lock = threading.Lock()
STR = "abc abc abc"
# print is not thread safe
def printer():
while True:
print STR
# lock print
def lock_printer():
while True:
with lock:
print STR
# logging is thread safe
def log():
while True:
logging.info(STR)
# print is not process safe
def process():
while True:
subprocess.Popen(['python', 'process.py', STR])
logging.basicConfig(level=logging.INFO)
threads = []
for i in range(10):
thread = threading.Thread(target=lock_printer)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
process.py
import sys
import time
import random
def main():
for i in range(5):
time.sleep(random.random())
print >> sys.stdout, sys.argv[1]
if __name__ == '__main__':
main()