import multiprocessing, sys, datetime

print_lock = multiprocessing.Lock()
def safeprint(content):
  with print_lock:
    sys.stdout.write(("[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'))

import multiprocessing, sys, datetime

print_lock = multiprocessing.RLock()
def safeprint(content):
  string = "[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'
  with print_lock:
    sys.stdout.write(string)

Reduce the amount of time locking
Reduce the amount of time locking
  import multiprocessing, sys, datetime
  
- print_lock = multiprocessing.Lock()
+ print_lock = multiprocessing.RLock()
  def safeprint(content):
+   string = "[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'
    with print_lock:
-     sys.stdout.write(("[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'))
+     sys.stdout.write(string)
  
Reduce the amount of time locking
## Code Before:
import multiprocessing, sys, datetime

print_lock = multiprocessing.Lock()
def safeprint(content):
  with print_lock:
    sys.stdout.write(("[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'))

## Instruction:
Reduce the amount of time locking
## Code After:
import multiprocessing, sys, datetime

print_lock = multiprocessing.RLock()
def safeprint(content):
  string = "[" + str(multiprocessing.current_process().pid) + "] " + datetime.datetime.now().strftime('%H%M%S') + ": " + str(content) + '\r\n'
  with print_lock:
    sys.stdout.write(string)
