0

Below is my code, but I cannot get anything to print anything in tasks_exec when running using Python IDLE or Jupyter.

import multiprocessing as mp
from multiprocessing import Process
from multiprocessing import Pool
import time

#Pool
tasks = (["A", 5], ["B", 2], ["C", 1], ["D", 3])

def tasks_exec(tasks_data):
    print(f'Process {tasks_data[0]} waiting {tasks_data[1]} seconds')
    time.sleep(int(tasks_data[1]))
    print(f'Process {tasks_data[1]} finished')
    return (tasks_data)


def pool_func():
    p = Pool(2)
    p.map(tasks_exec, tasks)
    print("This works")

if __name__ == '__main__':
    pool_func()
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Use backticks (on their own line), not single quotes, to start and end code segments -- and when you do so, you don't need to indent them. – Charles Duffy Mar 28 '21 at 18:53
  • That said -- when I run your code (saving it to a file and running `python3 that_file.py`), it works perfectly. All the "Process X waiting N seconds" messages print, and so does "This works". How exactly are you starting your program? – Charles Duffy Mar 28 '21 at 18:55
  • See your code also working perfectly at https://ideone.com/Ob3JxI (only change made is turning down the delays to not put more load than needed on the public service in question). Granted, maybe I turned the delays down a little _too_ far there, but the point (that the code in general works) should be considered made. – Charles Duffy Mar 28 '21 at 18:57
  • Also, note that for a lot of purposes `multiprocessing`'s overhead slows your program more than it speeds it. If you _can_ use multithreading instead (which is to say: if the GIL isn't your bottleneck), you generally should -- that way you don't pay serialization/deserialization costs to pass data around between processes. We have a _lot_ of questions here to the effect of "why did multiprocessing make my code slower?"; and the answer is frequently "your problem space isn't well-suited to its strengths". – Charles Duffy Mar 28 '21 at 19:00
  • Understood. I did see that in most cases it will make the code slower. I copied exactly what you ran in the provided link and it did the same as what I had, so that leads me to believe I am executing the correct code incorrectly... or something. I am using the Python 3.9.2 IDLE, I have also attempted to run using Jupyter, but get nothing from it. I am not sure what direction to look in to research and fix myself in that regard. – John Throneberry Mar 28 '21 at 22:01
  • The linked duplicate for Jupyter has some answers that actually make it work, vs the "save to a real `import`able module" easy answer. – Charles Duffy Mar 28 '21 at 23:07

0 Answers0