Python Forum
[PyQt] How to open a program with python without freezing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to open a program with python without freezing
#9
subprocess.run() is a blocking call, which returns stdout after the started program exited.
For your task (starting processes) is multiprocessing not needed and not helpful.
Instead of using subprocess.run, use subprocess.Popen which is not a blocking call.
It returns an object, where stdout, stderr and stdin is attached to.

from subprocess import Popen, PIPE


proc = Popen(['ls', '--help'], stdout=PIPE, stderr=PIPE, encoding='utf8', errors='ignore')
# use stdout
# use stderr
# stdin is not connected to the PIPE, this means no access to stdin
# encoding is utf8, which opens stdout, stderr and stdin in text mode.
# error are ignored

# proc.stdout.read() # blocks until the program finishes (EOF)
# proc.stdout.read(10) # blocks until 10 characters are available.
# proc.stdout.readline() # blocks until a line is available
You can also use select to look if data is available.
Handling the streams right with subprocess is not always easy.

If you need to wait for the whole output of the started program and do not want to block, you can start the proc from a Thread.
Attach a queue to the function and put the data into the queue.
On the other side you can use queue.get_nowait() on the queue to not to block.
The use of queue.get_nowait() need to require catching the exception queue.Empty.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How to open a program with python without freezing - by DeaD_EyE - Aug-14-2019, 07:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Open a python program from a button Pedroski55 3 5,191 Jul-20-2020, 11:09 PM
Last Post: Pedroski55
  [Tkinter] GUI keeps freezing Fre3k 2 3,687 May-23-2020, 05:41 PM
Last Post: Fre3k
  [Tkinter] How to create a delay for AI without freezing the GUI kom2 8 6,291 May-04-2019, 02:32 PM
Last Post: Yoriz
  Gi module window freezing problem loss 0 2,273 May-05-2018, 04:42 PM
Last Post: loss

Forum Jump:

User Panel Messages

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