title: How to sleep a process when using multiprocessing in Python 3.8.10 without using time.sleep()
body: <p>I am using around 1500 process multiprocessing.pool to run tasks parallely.</p>
  <p>In every process I need to perform a funtion every 5 mins, i.e every 5 mins I need to read 1500 files simultaneously.For every thread I am using time.sleep(300 - start_time) at the end of execution of function. However when trying to sleep only 16 process are executed because of 16 cores in my PC, rest all processes are not working.</p>
  <p>below is the code:</p>
  <pre><code>import logging
  import os
  import pandas as pd
  import datetime as dt
  os.chdir(os.path.dirname(os.path.abspath(__file__)))
  from scipy.stats import linregress
  from multiprocessing import Process, set_start_method, Pool, Value
  import multiprocessing as mp
  import traceback
  import atr_bb as ab
  import numpy as np
  from operator import getitem
  import datetime as dt
  import time
  import api as sa
  import requests
  
  def init(args):
      ''' store the counter for later use '''
      global counter
      counter = args
  
  
  def train(string, dones, left_after):
      global counter
  
      count=0
      t0 = dt.datetime.now()
      time.sleep(300.0 - (t0.minute%5*60 + t0.second))
      
      while(True):
          count+=1
          t0 = dt.datetime.now()
          if(t0.hour&gt;20):
              return
          
          print(string, &quot; &quot;, count)
          func(string)# function to be performed
          time.sleep(300.0 - (t0.minute%5*60 + t0.second) + 10)
  
          except:
              traceback.print_exc()
  
  
  if __name__ == '__main__':
      l = pd.read_csv('data.csv')['A']
      counter = Value('i', 0)
      mp.freeze_support()
      mp_manager = mp.Manager()
      data_dict = mp_manager.dict()
      dones = mp_manager.list()
      left = mp_manager.list()
      args = []
      for string in l:
          args.append((str(string), dones, left))
      
      p = Pool(initializer = init, initargs = (counter, ))
      p.starmap(train, args)
      p.close()
      p.join()
  </code></pre>
  <p>Now how can I pause a process for some time and CPU can be utilized by other processes?It seems time.sleep() is still using CPU and other strings are not processed totally.</p>
  <p>As per the answer2 posted by BooBoo, I have tried below code can you please suggest if it can be made faster.</p>
  <pre><code>import os
  os.chdir(os.path.dirname(os.path.abspath(__file__)))
  import logging
  import pandas as pd
  from scipy.stats import linregress
  from multiprocessing import Process, set_start_method, Pool, Value
  import multiprocessing as mp
  import traceback
  import atr_bb as ab
  import numpy as np
  from operator import getitem
  import datetime as dt
  import time
  import stock_api as sa
  import requests
  import asyncio
  import time
  import signal
  from multiprocessing import Manager
  import sys
  
  SLEEP_TIME = 5 * 60 # 5 minutes
  
  async def train(x, lock):
      loop = asyncio.get_running_loop()
      &quot;&quot;&quot;
      # I/O-bound portion:
      import aiofiles
      async with aiofiles.open(x, mode='r') as f:
          data = await f.read()
      # CPU-bound portion:
      loop.run_in_executor(None, train_part2, data)
      &quot;&quot;&quot;
      # but no I/O-bound portion for this demo:
  
      async with lock:
          data = get_data(x)
          
      loop.run_in_executor(None, train_part2, data, x)
      
  
  def train_part2(data, x):
      &quot;&quot;&quot; CPU-intensive portion &quot;&quot;&quot;
      print(x, &quot; &quot;, data.iloc[-1,-1]) # just print data
  
  async def dispatcher(x, lock):
      while(True):
          t0 = dt.datetime.now()
          if(t0.hour&gt;=15):
              return
          
          t1 = time.time()
          await train(x, lock)
          t2 = time.time()
          time_to_sleep = SLEEP_TIME - (t2 - t1)
          if time_to_sleep &gt; 0:
              await asyncio.sleep(time_to_sleep)
  
  async def main():
      l = pd.read_csv('data.csv')['A']
      lock = asyncio.Lock()
      t0 = dt.datetime.now()
      
      coroutines = ((dispatcher(x, lock)) for x in l)
      await asyncio.gather(*coroutines)
  
      t1 = dt.datetime.now()
      
      print(t1-t0)
      
  # Required for Windows:
  if __name__ == &quot;__main__&quot;:
      with Manager() as manager:
          asyncio.run(main())
  </code></pre>