简体   繁体   中英

Is there any way to check the progress on a Python script without interrupting the program?

Let's say I've written a script in python, program.py . I decide to run this in the Terminal using python program.py .

This code runs through an exceptional amount of data, and takes several hours to run.

Is there any way I can check on the status of this code without stoping the program?

You can try "strace" on the server where your script is running (assuming it is a Linux distro).

I am sure there are bunch of other ways to get this similar result but strace has been my friend.

Say here is my simple script:

[root@buzz tmp]# cat temp.py
#!/usr/bin/python
import time

for i in range(9999):
    print "Printing for %s time" % str(i)
    time.sleep(1)
[root@buzz tmp]#

And here is the strace output:

[root@buzz ~]# strace -p 10071
Process 10071 attached - interrupt to quit
select(0, NULL, NULL, NULL, {0, 736733}) = 0 (Timeout)
write(1, "Printing for 16 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
write(1, "Printing for 17 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
write(1, "Printing for 18 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
write(1, "Printing for 19 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
write(1, "Printing for 20 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0})     = 0 (Timeout)
write(1, "Printing for 21 time\n", 21)  = 21
select(0, NULL, NULL, NULL, {1, 0}^C <unfinished ...>
Process 10071 detached
[root@buzz ~]#

Ah the classic halting problem: is it really still running? There is no way to do this if you've already started the program, unless you've written in some debugging lines that check an external configuration for a debug flag (and I assume you haven't since you're asking this question).

You could look to the output or log of the script (if it exists), checking for signs of specific places in the data that the script has processed and thereby estimate the progress of the data processing.

Worst case: stop the thing, add some logging, and start it tonight just before bed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM