Python subprocess doesn't run until calling process finished -


edit 1 - added more code i'm not sure proc.communicate needed, 1 of suggestions found other stackoverflow code.(sorry tired last night , didn't think before asking question.)

i should add not experienced coder (mechanical engineer) can tell code

in gui have button call subprocess

the subprocess (screenshot-cmd.exe) creates png of cropped screen shot won't produce file until there error or if button click event over.

this makes me think subprocess not run until event finished

i want call process several times after single button press , move files produces after each 1 produced

if use proc.wait(), process hangs indefinitely.

how stop this?

# function take single image called 'filename' , place in directory 'dir' def takeimage(dir,filename):      # calculate view capture whole display window in.     clientrect = win32gui.getclientrect(win32gui.getforegroundwindow())     windowrect = win32gui.getwindowrect(win32gui.getforegroundwindow())     print(windowrect)     windowsize = [windowrect[2]-windowrect[0],windowrect[3]-windowrect[1]]     print(windowsize)     print(clientrect)      diffsize = [windowsize[0] -clientrect[2], windowsize[1] - clientrect[3]]     lrbborder = diffsize[0]/2     topborder = diffsize[1] - lrbborder      print("sizediff = " + str(diffsize))     windowname = win32gui.getwindowtext(win32gui.getforegroundwindow())     handleid = win32gui.getforegroundwindow()       leftmar = designlabel.getposition()[0] + lrbborder     topmar = designlabel.getposition()[1]  + topborder + designlabel.getsize()[1]     rightmar = leftmar + scene.width     bottmar = topmar+scene.height      margins = [leftmar,topmar,rightmar,bottmar]      print(margins)      # print view.     #command_line = r"screenshot-cmd -wt '" + windowname + "' -rc " + str(margins[0]) + " " + str(margins[1]) + " " + str(margins[2]) + " " + str(margins[3]) + " -o " + filename      command_line = r"screenshot-cmd -wt '" + windowname + "' -rc " + str(margins[0]) + " " + str(margins[1]) + " " + str(margins[2]) + " " + str(margins[3]) + " -o " + filename      print(command_line)     args = shlex.split(command_line)     proc = subprocess.popen(args)     proc.wait()       wx.yield()       if not os.path.isdir(dir):         os.makedirs(dir)      newpath = os.path.join(dir,filename)      if os.path.exists(newpath):         os.remove(newpath)      oldpath = os.path.join(os.getcwd(), filename)     print("old path: " + oldpath)     print("exists: " + str(os.path.exists(oldpath)))     shutil.move(oldpath,newpath)      return  #event called upon clicking 'taketenimag' button def taketenimge(evt):     global designno     global workingdirectory     global numdesigns     filenameroot = "test_"     fileextention = ".png"      # check there @ least 10 designs     if numdesigns > 9 , os.path.exists(workingdirectory):         # find directory path put images in          dir = os.path.join(workingdirectory, "images")         # each design          x in range(10):             print("design =" + str(designno))             filename = filenameroot + str(designno) + fileextention             print("------------------")             print("for x = " + str(x) + " "  + filename)             #   create image , save             print(dir)             takeimage(dir,filename)             #move next design              wx.postevent(forwarddesign, wx.commandevent(wx.wxevt_command_button_clicked, forwarddesign.getid()) )              wx.yield()             print("design =" + str(designno)) return  taketenimg = wx.button(p, label='take ten images', pos=(rb + visscaletext.getsize()[0]+10,takeimg.getposition()[1]+5 +takeimg.getsize()[1]), size = (100,30)) taketenimg.bind(wx.evt_button, taketenimge) 

https://code.google.com/p/screenshot-cmd/

barnaby, may over-complicating subprocess use. popen typically used when need communicate process during time running. sound of it, don't need that, might want use higher level function. see the docs on subprocess' various invocations, , perhaps try using call method. you'll need shell=true, detailed in questions such this one.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -