# Inconsistent handling of multiprocessing by slicer

**URL:** <https://discourse.slicer.org/t/inconsistent-handling-of-multiprocessing-by-slicer/24623>\
**Category:** Support\
**Tags:** python\
**Created:** [August 3, 2022, 4:56pm UTC](https://discourse.slicer.org/t/inconsistent-handling-of-multiprocessing-by-slicer/24623 "2022-08-03T16:56:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dvbower](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dvbower](https://discourse.slicer.org/u/dvbower)\
**Post date:** [August 3, 2022, 4:56pm UTC](https://discourse.slicer.org/t/inconsistent-handling-of-multiprocessing-by-slicer/24623/1 "2022-08-03T16:56:27Z")

</div>

On a Windows 10 enterprise computer the following multiprocessing code block evaluates differently for two execution commands. Executing the script directly with `PythonSlicer.exe` gives the expected result, i.e., output is reported for both `main` and `fun`. However, executing the script with `slicer.exe --python-script [script]` only reports output from `main`, suggesting that `fun` has not been called, thereby implying a problem with the multiprocessing. In contrast, on a Mac, both execution approaches (either using python directly, or piping through slicer) return output from both `main` and `fun`.

I mostly want to execute the python script using slicer.exe to access the ctk and slicer modules, which I don’t believe I can access from the PythonSlicer.exe python bundle. Any suggestions are much appreciated.

```
import sys
import time
from multiprocessing import Process

def fun():

    outfile = open('test_fun.log','w')
    print("Starting fun")
    outfile.write('Starting fun\n')
    time.sleep(2)
    outfile.write('Finishing fun')
    print('Finishing fun')
    outfile.close()

def main():

    p = Process(target=fun)
    p.start()
    p.join()

if __name__ == ' __main__':

    print('Starting main')
    outfile = open('test_main.log', 'w')
    main()
    outfile.write('Yes in main()')
    outfile.close()
    print('Finishing main')
    sys.exit(0)

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 3, 2022, 5:51pm UTC](https://discourse.slicer.org/t/inconsistent-handling-of-multiprocessing-by-slicer/24623/2 "2022-08-03T17:51:55Z")

</div>

There could definitely be differences between `--python-script`, which happens in the slicer application main loop, and running in the `PythonSlicer` interpreter with regards to `multiprocessing`. In the context of the Slicer main loop you might have more luck with `QProcess` like we use in the [SlicerParallelProcessing](https://github.com/pieper/SlicerParallelProcessing) extension.

---

<div class="post-metadata">

**Author:** ![dvbower](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dvbower](https://discourse.slicer.org/u/dvbower)\
**Post date:** [August 4, 2022, 8:02am UTC](https://discourse.slicer.org/t/inconsistent-handling-of-multiprocessing-by-slicer/24623/3 "2022-08-04T08:02:38Z")

</div>

Thanks I will check it out.
