0

I would like to make use of parallelization in my program by using the python multiprocessing library. For that purpose I want to check whether an action is right doable right now and if not yield the quantum of the current process and allow the next process to make use of the cpu time until the scheduler decides to give it another try.

By searching I have found that time.sleep(0) seems to be doing it for threads. Does it also work for processes?

  • Usually. The unit of execution on windows and unix like systems is usually the thread. Sleep one in any process and you return to the operating system scheduler. `sleep(0)` is agressive polling and is usually not the right thing to do. Ideally, there is a wait to wait on a resource somehow - but that depends on the thing you are waiting for. – tdelaney Apr 25 '21 at 23:46
  • I am waiting for a zeromq message via the .recv function. This function is blocking and I want to make sure that the cpu-time isn't wasted if the message arrives late. I was planning to poll for a short amount of time whether a message is available to be received and yielding the quantum if no arrival of a message happens. – Learner Apr 25 '21 at 23:53
  • I think the wait function blocks, which releases the thread, allowing other threads / processes in the system to run. I don't see a need for a sleep(0). – tdelaney Apr 26 '21 at 00:18
  • Do you mean that there is a wait function in the recv() method which blocks and releases the thread? So in the end I don't have to change anything? – Learner Apr 26 '21 at 00:35
  • The zeromq recv function waits for data. But this is the opposite of "release the thread". The thread is suspended, waiting on an operating system resource such as a semaphore or event object. Since the thread is suspended, the CPU will run other threads. When data arrives, zeromq will release the resource, the thread will exit the wait and it will be available to be run by the CPU again. – tdelaney Apr 26 '21 at 01:41
  • Thank you! That helps a lot! Do you have a source for me where I can cite this for my work? Unfortunately, I haven't found anything in the ZeroMQ guide that indicates this. – Learner Apr 26 '21 at 02:09
  • Here's a link to recv: https://pyzmq.readthedocs.io/en/latest/api/zmq.html#zmq.Socket.recv. "With flags=NOBLOCK, this raises ZMQError if no messages have arrived; otherwise, this waits until a message arrives. See Poller for more general non-blocking I/O." – tdelaney Apr 26 '21 at 02:49
  • This is pretty standard for any type of os resource similar to a file or socket. For example, reading from your hard drive, the OS will send messages to the necessary hardware to initiate the transaction, and go off and do other things while the sata hardware works in the meantime. When it's done the sata hardware will send an interrupt to the cpu to tell it the data is ready in some buffer somewhere. The cpu will then be able to schedule the thread to resume, and go fetch that buffer. – Aaron Apr 26 '21 at 14:20

0 Answers0