def play_nice_with_threads(sleep=True, weak=False, deadline=None):
 if (weak or (threading.activeCount() < 4)):
  time.sleep(0)
  return 0
 deadline = ((time.time() + 5) if (deadline is None) else deadline)
 while True:
  activity_threshold = (((180 - time.time()) + LAST_USER_ACTIVITY) / 120)
  delay = max(0.001, min(0.1, (0.1 * activity_threshold)))
  if (not sleep):
   break
  if (LIVE_USER_ACTIVITIES < 1):
   time.sleep(delay)
  else:
   time.sleep(max(delay, 0.25))
  if (QUITTING or (LIVE_USER_ACTIVITIES < 1)):
   break
  if (time.time() > deadline):
   break
 return delay

def play_nice_with_threads ( sleep = True , weak = False , deadline = None ) : if ( weak or ( threading . activeCount ( ) < 4 ) ) : time . sleep ( 0 ) return 0 deadline = ( ( time . time ( ) + 5 ) if ( deadline is None ) else deadline ) while True : activity_threshold = ( ( ( 180 - time . time ( ) ) + LAST_USER_ACTIVITY ) / 120 ) delay = max ( 0.001 , min ( 0.1 , ( 0.1 * activity_threshold ) ) ) if ( not sleep ) : break if ( LIVE_USER_ACTIVITIES < 1 ) : time . sleep ( delay ) else : time . sleep ( max ( delay , 0.25 ) ) if ( QUITTING or ( LIVE_USER_ACTIVITIES < 1 ) ) : break if ( time . time ( ) > deadline ) : break return delay
long-running batch jobs should call this now and then to pause their activities in case there are other threads that would like to run .