WebDev: "Merged" result from previous or simultan call

Hello,

we have sometimes a problem with Webdev. If a request from 2 different applications to the same API, the results are sometimes wrong.

Tested on Ignition Version : 8.17 and 8.13

Here i have a simple Example :
The script first adds a empty array via function “test.setResult” (see code below)
and add the postdata to the added array.

test.setResult Function :

Now i call the the function remotly via script console, i do it 2 times with system.util.invokeAsynchronous
to simulate nearly a “call at same time”

the result in from print statement :

As you can see the Arrays are somtimes merged. This behaviour we are not expecting.
Is it a Bug?

if we change this line in Webdev

res = test.setResult( testdata = [('TEST', [])])

to

res = test.setResult(res = {}, testdata = [('TEST', [])])

it seems to work. So for me it looks like the “res” is somehow cached in the server.

But we are not sure if the problem is 100% solved. So maybe someone have a Tipp what we are doing wrong or our thinking is wrong :).

Thank you in advance :slight_smile:
Br,
Roland

I am pretty sure this

def setResult(res={}, testdata=[])

is the issue.

Try:

def setResult(res=None, testdata=None):
    if res is None:
        res={}
    if testdata is None:
        testdata=[]
    for elem in testdata:
        res[elem[0]] = elem[1]
    return res

Check this out - Do not use mutable objects as default arguments in Python | by Timur Bakibayev | Geek Culture | Medium

Typically it’s a bad idea to use a list, set, dictionary, as a default keyword argument value. Better off using None and then initializing if it is none.

4 Likes

For such an application, I would not use default arguments at all. Always pass both arguments.

4 Likes

Hi @bkarabinchak.psi
Thank you!
Works like a charm.

@pturmel i will follow youre info thanks.

1 Like

For this, I would actually not make a function at all.

here’s what I’d suggest:

a is the dict, b is the list containing tuples:

a = {
	'foo': "FOO",
	'bar': "BAR"
}

b = [
	('baz', "BAZ"),
	('bla', "BLA")
]

in the case where you want to add b to a:

a.update(b)

if you want a new dict with items from b:

new_dict = dict(b)

Python makes things so easy people always find ways to make it more complicated than it needs to be !

2 Likes