Python Forum
Pausing a loop with spacebar, resume again with spacebard
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pausing a loop with spacebar, resume again with spacebard
#3
(May-25-2017, 09:50 PM)Larz60+ Wrote: when you run your Popen, it should be done like:
from the docs: https://docs.python.org/3.6/library/subprocess.html
Quote:Popen.communicate(input=None, timeout=None)

   Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or None, if no data should be sent to the child. If streams were opened in text mode, input must be a string. Otherwise, it must be bytes.

   communicate() returns a tuple (stdout_data, stderr_data). The data will be strings if streams were opened in text mode; otherwise, bytes.

   Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

   If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output.

   The child process is not killed if the timeout expires, so in order to cleanup properly a well-behaved application should kill the child process and finish communication:

    proc = subprocess.Popen(...)
    try:
        outs, errs = proc.communicate(timeout=15)
    except TimeoutExpired:
        proc.kill()
        outs, errs = proc.communicate()
   Note:
   The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

   Changed in version 3.3: timeout was added.


I tried implementing the code the way the docs provided by I"m still not fully understanding it, nor exactly how I can apply p.communicate to do what I want to do.

from PIL import Image
import time
import subprocess
from msvcrt import getch
for i in bio:
   p = subprocess.Popen(["C:\Program Files\IrfanView\i_view64.exe", 'C:\\Users\Moondra\\Bioteck_charts\{}.png'.format(i)])
   key = ord(getch())
   try:
       outs, errs = p.communicate()
       if key == 32:
           ???
       else: 
           time.sleep(5)
           p.kill()
       print(outs)
   except Exception as e:
       p.kill()
       outs, errs = p.communicate() 
It opens up the image, but I have to manually close the image.
Once, I close the image(pressing x in the corner of the window), the next image isn't popping up and the program hangs in Jupyter Notebook.

I'm assuming outs is some sort of output data. It's not really printing  anything despite explicitly writing print.
Reply


Messages In This Thread
RE: Pausing a loop with spacebar, resume again with spacebard - by bigmit37 - May-26-2017, 12:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to create one resume parser api using Python Flask parthsukhadiya 1 942 Nov-28-2023, 05:07 PM
Last Post: Pedroski55
  Pausing and returning to function? wallgraffiti 1 2,194 Apr-29-2021, 05:30 PM
Last Post: bowlofred
Question resume file transfer onran 0 1,704 Jan-27-2021, 02:16 PM
Last Post: onran
  Slide show with mouse click pausing aantono 1 2,250 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  Pausing a running process? MuntyScruntfundle 2 7,346 Nov-14-2018, 05:14 PM
Last Post: woooee
  Pausing a Script malonn 4 3,867 Aug-04-2018, 07:58 PM
Last Post: malonn
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,822 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  Pausing and playing a while loop. traceon 1 3,931 Jan-24-2017, 09:11 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