# Multithreaded in ignition

**URL:** <https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476>\
**Category:** Ignition\
**Tags:** vision, scripting, database, ignition80\
**Created:** [April 12, 2021, 6:39am UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476 "2021-04-12T06:39:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [April 12, 2021, 6:39am UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/1 "2021-04-12T06:39:15Z")

</div>

I use ignition to import the customer data (first convert it to a csv format), into my database.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/8/78f7ce4c84ee2720ca7bfed1d3eb3d09f1f4561c.png)  
while during the software is upload the data to my database, the client is not able to do other things

```auto
path = system.file.openFile("csv")
data_string = system.file.readFileAsString(path)

# Convert the string to a dataset and store in a variable
data = system.dataset.fromCSV(data_string)
# Assign the dataset to a table
query="""INSERT INTO [dbo].[HYText]
           ([THBH]
           ,[Project]
           ,[ZCBH]
           ,[ZCMX]
           ,[UpdateTime])
     VALUES
	           (?,?,?,?,SYSDATETIME())
	           """
py_TableData = system.dataset.toPyDataSet(data)
system.gui.messageBox(u'请注意，数据在上传中， 请稍后')
for row in py_TableData:
	args = [row[2], row[3],row[4],row[5]]
	system.db.runPrepUpdate(query, args)

```

the upside is the logic, how to optimize the logic then while I am upload the data, I can do some other things?

---

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [April 12, 2021, 7:13am UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/2 "2021-04-12T07:13:02Z")

</div>

use system.util.invokeAsynchronous() in vision client side.  
For perspective, all scripts run on the gateway side so this function doesn’t work.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 12, 2021, 12:14pm UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/3 "2021-04-12T12:14:28Z")

</div>

This seems like a good candidate to be doing in a gateway script. Vision clients have one GUI thread. If you’re running this on button or something, then until it completes, the GUI will be unusable.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 12, 2021, 12:27pm UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/4 "2021-04-12T12:27:36Z")

</div>

@nader.chinichian is correct, to call this on a background thread you would use `system.util.invokeAsynchronous()`. as for optimization, consider inserting multiple rows with a single query, as opposed to calling the query each time through a loop.

I would also avoid the un-needed conversion to a pyDataset, but that’s more personal preference than any real performance gain.

I would refactor the code to look something like this:

```auto
path = system.file.openFile('csv')
data_string = system.file.readFileAsString(path)

# Convert the string to a dataset and store in a variable
data = system.dataset.fromCSV(data_string)

#build the query
query = """INSERT INTO [dbo].[HYText]
                    ([THBH]
                    ,[Project]
                    ,[ZCBH]
                    ,[ZCMX]
                    ,[UpdateTime])
                VALUES %s"""

valueArgs = ','.join(['(?,?,?,?,SYSDATETIME())'] * data.rowCount)

system.gui.messageBox(u'请注意，数据在上传中， 请稍后')

argList = [data.getValueAt(row,col) for row in range(data.rowCount) for col in range(2,6)]

system.db.runPrepUpdate(query % valueArgs,argList)

```

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [April 13, 2021, 4:49am UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/5 "2021-04-13T04:49:39Z")

</div>

Thanks for your code improve

---

<div class="post-metadata">

**Author:** ![yifeiliu08](https://avatars.discourse-cdn.com/v4/letter/y/bbe5ce/32.png) [@yifeiliu08](https://forum.inductiveautomation.com/u/yifeiliu08)\
**Post date:** [April 13, 2021, 4:49am UTC](https://forum.inductiveautomation.com/t/multithreaded-in-ignition/45476/6 "2021-04-13T04:49:53Z")

</div>

thanks a lot
