Python Forum

Full Version: count certain task in task manager[solved]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, sorry for my bad English
I creating a downloader to download multiple files at once using python,
I will not write details, this is the part:
if not os.path.exists(SaveAs):
    time.sleep(1.0) # download size average is under 1 Mb
    Calling = 'python.exe', "DownloadSingle.py", URL, SaveAs
    subprocess.Popen(Calling)
now my problem is: that sometimes, the file is over 1Mb, and the subprocess keeps increasing,
[attachment=1946]
I want to limit it by using a script like this(just an idea)

while taskmanager.count("Python") > 4:
     time.sleep(1.0)
how do I do this?
thank you, have a nice day
I just find the correct keyword: python get all running task manager
Can use psutil for this.
Example.
import psutil

count = 0
PROCNAME = "python.exe"
for proc in psutil.process_iter():
        if proc.name() == PROCNAME:
            count += 1

print(count)
Output:
3
(Aug-29-2022, 04:29 PM)kucingkembar Wrote: [ -> ]I want to limit it by using a script like this(just an idea)
while taskmanager.count("Python") > 4:
    time.sleep(1.0) 
how do I do this?
Don't use time.sleep() when what to schedule at task as certain interval,use eg schedule
Look at my post here,you can rewrite it a little to check that just a limited of prosses is spawned.
(Aug-29-2022, 04:58 PM)snippsat Wrote: [ -> ]Can use psutil for this.
Example.
import psutil

count = 0
PROCNAME = "python.exe"
for proc in psutil.process_iter():
        if proc.name() == PROCNAME:
            count += 1

print(count)
Output:
3
(Aug-29-2022, 04:29 PM)kucingkembar Wrote: [ -> ]I want to limit it by using a script like this(just an idea)
while taskmanager.count("Python") > 4:
    time.sleep(1.0) 
how do I do this?
Don't use time.sleep() when what to schedule at task as certain interval,use eg schedule
Look at my post here,you can rewrite it a little to check that just a limited of prosses is spawned.
thank you for the reply, I used that medhod,
I use time.sleep() because I download using for loop, if file do not exist : take a breath 1second ,then download