Python Forum
multiprocessing difference between Linux and Windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiprocessing difference between Linux and Windows
#1
I was trying to accept timed input when I noticed that Windows did not behave as I expected. A simplified version of the code to demonstrate this is below:
import multiprocessing, sys, os

def _input_with_timeout_process(stdin_file_descriptor, queue, prompt):
    sys.stdin = os.fdopen(stdin_file_descriptor)
    queue.put(input(prompt))
    return

print("=== 1 ===", 'parent process:', os.getppid(), 'my pid:', os.getpid())

if __name__ == '__main__':
    queue = multiprocessing.Queue()
    process = multiprocessing.Process(target=_input_with_timeout_process, 
        args=(sys.stdin.fileno(), queue, "Say something: "))
    process.start()
    try:
        process.join(10)
        if process.is_alive():
            raise ValueError("Timed out waiting for input.")
        print("I saw:", queue.get())
    except ValueError:
        print("Sorry, too slow.")
    finally:
        process.terminate()
Running this with Python 3.8.2 on Ubuntu 20.04 gives:
Output:
direl@MD-3670:~/Shared$ python min_mp_test.py === 1 === parent process: 30833 my pid: 32234 Say something: g I saw: g direl@MD-3670:~/Shared$
However on Windows 10 with Python 3.8.6 I get
Output:
PS C:\Users\mdavi\Documents> python z:min_mp_test.py === 1 === parent process: 4288 my pid: 5408 === 1 === parent process: 5408 my pid: 5764 Say something: f I saw: f PS C:\Users\mdavi\Documents>
It looks as though the second process is escaping from its function definition and executing any subsequent code that is not in either a function definition or an
if __name__ == '__main__': statement.

While making sure that everything is indented under something will avoid the problem I was curious if this is actually the correct or even accepted behaviour. Note that the original program did not have the if __name__ == '__main__': and its behaviour was a lot stranger.
Reply


Messages In This Thread
multiprocessing difference between Linux and Windows - by direl - Oct-01-2020, 11:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,367 May-24-2022, 07:02 AM
Last Post: netanelst
  Resources for printing tabledata-Linux and Windows hammer 4 1,476 Apr-25-2022, 12:09 PM
Last Post: hammer
  script works in windows but not in linux ovidius80 2 2,780 Apr-29-2020, 02:10 PM
Last Post: ovidius80
  keyboad scanner routine(linux and windows) 1885 0 1,935 Oct-26-2019, 03:34 PM
Last Post: 1885
  Coloured text in the terminal works in Linux but not Windows Flexico 2 2,750 Aug-26-2019, 10:44 PM
Last Post: Flexico
  Python files made on linux don't work on windows sylas 2 3,299 Oct-10-2018, 05:34 AM
Last Post: sylas
  can import msvcrt and click on windows but not on Linux sylas 4 13,367 Aug-06-2018, 08:28 AM
Last Post: Gribouillis
  Windows/linux shell emulation AceScottie 3 3,486 Nov-27-2017, 01:31 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020