Python Forum
Launch pdf and close on delete window event
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Launch pdf and close on delete window event
#4
There is something it doesn't like about the pid and I don't know where the pid is coming from. Because we use what we know, I use multiprocessing (example below) because I know multiprocessing works, and because multiprocessing will use multiple cores when available, so try modifying this code to fit you, and see if it works any better, and obviously post back if it doesn't.
## very basic and simple but should do the trick

import multiprocessing
import os
import psutil
import time


def killtree(pid, including_parent=True):
    parent = psutil.Process(pid)
    for child in parent.children(recursive=True):
        print("killing child", child)
        child.kill()
    if including_parent:
        parent.kill()

def print_numbers(spaces):
    ctr = 0
    for x in range(11):
        ctr +=1
        print(" "*spaces, ctr)
        time.sleep(0.5)

## do this first as it gets the latest pid started
## should be the pid of this program
## if you wait and the OS starts another process, you wil get that pid
pid=os.getpid()

list_of_multis=[]
for ctr in range(5):
    list_of_multis.append(multiprocessing.Process(target=print_numbers, args=(ctr,)))
    list_of_multis[-1].start()

## wait 2 seconds and kill all processes
time.sleep(2)
killtree(pid) 

And I got so caught up in psutil that I didn't post terminating each process. This is better than psutil because you can join() each process, which allows anything in the background to be cleaned up.
import multiprocessing
import time

def print_numbers(spaces):
    ctr = 0
    for x in range(11):
        ctr +=1
        print(" "*spaces, ctr)
        time.sleep(0.5)

list_of_multis=[]
for ctr in range(5):
    list_of_multis.append(multiprocessing.Process(target=print_numbers, args=(ctr,)))
    list_of_multis[-1].start()

## wait 2 seconds and kill all processes
time.sleep(2)
for process in list_of_multis:
    process.terminate()
    process.join()
    print(process, "terminated")
Reply


Messages In This Thread
RE: Launch pdf and close on delete window event - by woooee - Mar-21-2018, 06:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 609 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] command require close window Krissstian 14 3,077 Nov-19-2022, 04:18 PM
Last Post: Krissstian
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,804 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 6,794 Nov-02-2019, 12:18 PM
Last Post: 1885
  Using tkinter on Windows to launch python 3 scripts Mocap 1 2,767 Jul-17-2019, 05:16 AM
Last Post: Yoriz
  update a variable in parent window after closing its toplevel window gray 5 9,173 Mar-20-2017, 10:35 PM
Last Post: Larz60+
  pygtk2, how to disconnect all callback of widget or window event ? harun2525 1 3,343 Feb-19-2017, 11:44 PM
Last Post: Larz60+
  [Tkinter] I have a Toplevel button in tkinker that I want to close the window and then perform Bloodypizza17 2 7,842 Jan-06-2017, 07:18 PM
Last Post: Bloodypizza17
  launch .PY program Help iw2fo 22 20,261 Oct-18-2016, 09:13 PM
Last Post: Barrowman

Forum Jump:

User Panel Messages

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