Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 538
ShanZhengYang Avatar asked Oct 18 '22 21:10

ShanZhengYang


1 Answers

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 ~]#
like image 59
gixxer Avatar answered Oct 21 '22 15:10

gixxer