sql - make python wait for stored procedure to finish executing -
i have python script uses pyodbc call mssql stored procedure, so:
cursor.execute("exec myprocedure @param1 = '" + myparam + "'")
i call stored procedure inside loop, , notice sometimes, procedure gets called again before finished executing last time. know because if add line
time.sleep(1)
after execute line, works fine.
is there more elegant , less time-costly way say, "sleep until exec finished"?
update (divij's solution): code not working me:
from tornado import gen import pyodbc @gen.engine def func(*args, **kwargs): # connect db cnxn_str = """ driver={sql server native client 11.0}; server=172.16.111.235\sqlexpress; database=celltestdata2; uid=sa; pwd=welcome!; """ cnxn = pyodbc.connect(cnxn_str) cnxn.autocommit = true cursor = cnxn.cursor() _ in range(5): yield gen.task(cursor.execute, 'exec longtest') return func()
there's no python built-in allows wait asynchronous call finish. however, can achieve behaviour using tornado's ioloop. tornado's gen
interface allows register function call task
, return next line in function once call has finished executing. here's example using gen
, gen.task
from tornado import gen @gen.engine def func(*args, **kwargs) _ in range(5): yield gen.task(async_function_call, arg1, arg2) return
in example, execution of func
resumes after async_function_call
finished. way subsequent calls asnyc_function_call
won't overlap, , wont' have pause execution of main process time.sleep
call.
Comments
Post a Comment