# Asynchronously execute Stored Procedures via Gateway Script

**URL:** <https://forum.inductiveautomation.com/t/asynchronously-execute-stored-procedures-via-gateway-script/9046>\
**Category:** Ignition\
**Created:** [September 11, 2014, 6:06pm UTC](https://forum.inductiveautomation.com/t/asynchronously-execute-stored-procedures-via-gateway-script/9046 "2014-09-11T18:06:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxanemi](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@oxanemi](https://forum.inductiveautomation.com/u/oxanemi)\
**Post date:** [September 11, 2014, 6:06pm UTC](https://forum.inductiveautomation.com/t/asynchronously-execute-stored-procedures-via-gateway-script/9046/1 "2014-09-11T18:06:34Z")

</div>

I want to execute two Stored Procedures asynchronously i.e., one after the another.

SP\_COATING\_DASH\_2 executes in 4.6 secs and  
SP\_SET\_LOCK\_DASH executes in less than 1 sec.

In my script, I have:

```auto
## Code for running SP's to DAILY_DASH
## At 11:59 AM on every Thursday Night
## The gateway script should check every one min OR @60,000 ms
import system
import time
from datetime import datetime

dateFormat ="%Y-%m-%d %H:%M:%S"

currentDate = system.db.dateFormat(system.tag.read("CurrentDateTime").value,"yyyy-MM-dd HH:mm:ss") ## Get date from the Tag
## pythonDate = datetime.now();

currentDatePython = datetime.strptime(currentDate,dateFormat) ## Convert the date to datetime format 

currentDayOfWeek = currentDatePython.weekday() ##{Mon = 0, Tue=1, Wed = 2, *Thu = 3*, Fri = 4, Sat = 5, Sun = 6}
currentHour = currentDatePython.hour ## Range(0,24)
currentMin = currentDatePython.minute ## Range (0,60)
currentSec = currentDatePython.second ## Range (0,60) ## We may not need seconds in this code

if currentDayOfWeek == 3: ##Thursday
	if currentHour == 23: ## 11 AM
		if currentMin == 59: ## 11:59 AM
			##Create SP Calls here
			coater = system.db.createSProcCall("SP_COATING_DASH_2","OPFIS")
			setLockToOne = system.db.createSProcCall("SP_SET_LOCK_DASH","OPFIS")
			
			## Add SP Calls for other Stored Procedures as well here
			system.db.execSProcCall(coater)
			## The lock should be set to 1 after all these stored procedures are called!
			time.sleep(1) # sleep for 1s
			system.db.execSProcCall(setLockToOne) ## Set Lock = 1 to all the rows
		

```

Should I use the time.sleep function (or) execute it using the invokeLater? This is a gateway timer script that runs @ every 60,000 ms and checks whether it is Thursday, 11:59 AM and updates the tables!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [September 11, 2014, 8:49pm UTC](https://forum.inductiveautomation.com/t/asynchronously-execute-stored-procedures-via-gateway-script/9046/2 "2014-09-11T20:49:00Z")

</div>

Hello oxanemi,

Your script is executing the stored procedures synchronously, not asynchronously. To execute them asynchronously you need to use [system.util.invokeAsynchronous](https://inductiveautomation.com/support/usermanuals/ignition/index.html?system_util_invokeasynchronous.htm).

You can’t use system.util.invokeLater in this script because this function only exists in Clients and Designers. It does not exist in the Gateway scope. You can use time.sleep(1).

Best,
