简体   繁体   中英

Python: Get current line runtime

In Python how to get what block of script is running just right now, runtime ?

print a()             #takes couple of seconds, 3s
print b()             #takes couple of seconds, 3s
pass
print c()             #takes couple of seconds, 4s
print d(), \
      t,y,u,i,o       #takes couple of seconds, 4s
print z

So to know ( report ) what is running in the second 7th, for example.

report(7s): print c()

EDIT:

We keep the above as it is to justify the comments given ;) however below we describe our questions in more details.

A Python code is being executed line by line (or better block by block). See for example:

in code.py :

for i in xrange(10000000):                #assume this will take some seconds
   pass

print 'something'
#another time consuming job
for j in xrange(10000000):                #assume this will also take some seconds
   pass

print 'another thing'

We were thinking having a time thread which samples every 5 seconds to print (ie, report) where we are in the code in runtime . So to have every 5 seconds printing what just happening during the execution.

Example output:

>>> 00:05 in progress: "for i in xrange(10000000):..."
>>> 00:10 in progress: "for i in xrange(10000000):..."
>>> 00:15 in progress: "for j in xrange(10000000):..."
...
def fowia(signal, frame):
    print frame.f_lineno


import signal
signal.signal(signal.SIGHUP, fowia)

for i in xrange(10000000):                
    for k in xrange(100000000):
       pass

print 'something'
#another time consuming job
for j in xrange(10000000):                
        pass
print 'another thing'

To use this simple example, start it running then find the pid number of the process from another shell. Then send a HUP signal to the process. It will print the current line number. See http://docs.python.org/2/library/inspect.html for a list of other program source introspection methods that can be used

fowia = find out where I am, btw :)

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