Time.sleep(0.5) to delay next line of code from running approach

I have been reading that time.sleep() is not an ideal approach to be used in ignition but it seems to be the way to resolve my 'lag' issue.

last = len(results)-1
self.getChild("table").props.data = system.dataset.toDataSet(results)

time.sleep(0.5)
self.getChild("table").props.selection.selectedRow = last

This will work 80% of the time where my table selected row gets highlighted.

However, removing time.sleep() will sometimes make my selected row highlighted work say 30% of the time.

Also, on initial load from a page navigation, sometime it would not even highlight the selected row. I am not sure if this can be related to network issues but a refresh will fix it.

I want to know if there is another approach where I would not need to use time.sleep().

How often is your table data changing?

You could possibly put a change event script on the table's data property that selects the last row. That would make sure the selection happens after the new data is loaded.

You could do the same thing with an expression binding on the table's selectedRow property. Something like len({self.props.data} -1) would only execute if the table's data changed, and let the user select rows.

Try putting the the selection in an onChange script for props.data. Though still not ideal, using a Timer is preferable to .sleep():

from threading import Timer

def selectLastRow(self, last):
    self.props.selection.selectedRow = last

last = len(results)-1
self.getChild("table").props.data = system.dataset.toDataSet(results)
Timer(0.2, selectLastRow, [self, last]).start()

In the code that you posted, there is no need for a "delay" to keep the next line of code from running. system.dataset.toDataSet() is a blocking function, meaning that it will already wait until it is done processing before moving on to the next line. So in this example, sleep() is not giving you anything but worse performance.

What exactly are you wanting to happen? Do you always want the last row to be the selected row? Why?

Ignition is an event driven environment, so you should be asking, What event occurs that will result in this row being selected?

Example view of using a binding to do this:

view's json
{
  "custom": {},
  "params": {},
  "props": {},
  "root": {
    "children": [
      {
        "meta": {
          "name": "Dropdown_0"
        },
        "position": {
          "basis": "36px"
        },
        "propConfig": {
          "props.options": {
            "binding": {
              "config": {
                "path": "../Table.custom.data"
              },
              "transforms": [
                {
                  "code": "\treturn [\n\t\t{\n\t\t\t\u0027label\u0027: n,\n\t\t\t\u0027value\u0027: n\n\t\t} for n in xrange(len(value))\n\t]",
                  "type": "script"
                }
              ],
              "type": "property"
            }
          }
        },
        "props": {
          "value": 4
        },
        "type": "ia.input.dropdown"
      },
      {
        "custom": {
          "data": [
            [
              {
                "city": "Folsom",
                "country": "United States",
                "population": 77271
              },
              {
                "city": "Helsinki",
                "country": "Finland",
                "population": 635591
              },
              {
                "city": "Jakarta",
                "country": "Indonesia",
                "population": 10187595
              },
              {
                "city": "Madrid",
                "country": "Spain",
                "population": 3233527
              },
              {
                "city": "Prague",
                "country": "Czech Republic",
                "population": 1241664
              },
              {
                "city": "San Diego",
                "country": "United States",
                "population": 1406630
              },
              {
                "city": "San Francisco",
                "country": "United States",
                "population": 884363
              },
              {
                "city": "Shanghai",
                "country": "China",
                "population": 24153000
              },
              {
                "city": "Tokyo",
                "country": "Japan",
                "population": 13617000
              },
              {
                "city": "Washington, DC",
                "country": "United States",
                "population": 658893
              }
            ],
            [
              {
                "city": "Wellington",
                "country": "New Zealand",
                "population": 405000
              },
              {
                "city": "Delhi",
                "country": "India",
                "population": 11034555
              },
              {
                "city": "Dhaka",
                "country": "Bangladesh",
                "population": 14399000
              },
              {
                "city": "Lagos",
                "country": "Nigeria",
                "population": 16060303
              },
              {
                "city": "Karachi",
                "country": "Pakistan",
                "population": 14910352
              },
              {
                "city": "Istanbul",
                "country": "Turkey",
                "population": 14025000
              },
              {
                "city": "Cairo",
                "country": "Egypt",
                "population": 10230350
              },
              {
                "city": "Mexico City",
                "country": "Mexico",
                "population": 8974724
              },
              {
                "city": "London",
                "country": "United Kingdom",
                "population": 8825001
              },
              {
                "city": "New York City",
                "country": "United States",
                "population": 8622698
              }
            ],
            [
              {
                "city": "Tehran",
                "country": "Iran",
                "population": 8154051
              },
              {
                "city": "Bogota",
                "country": "Colombia",
                "population": 7878783
              },
              {
                "city": "Rio de Janeiro",
                "country": "Brazil",
                "population": 6429923
              },
              {
                "city": "Riyadh",
                "country": "Saudi Arabia",
                "population": 5676621
              },
              {
                "city": "Singapore",
                "country": "Singapore",
                "population": 5535000
              },
              {
                "city": "Saint Petersburg",
                "country": "Russia",
                "population": 5191690
              },
              {
                "city": "Sydney",
                "country": "Australia",
                "population": 208374
              },
              {
                "city": "Abidjan",
                "country": "Ivory Coast",
                "population": 4765000
              },
              {
                "city": "Dar es Salaam",
                "country": "Tanzania",
                "population": 4364541
              },
              {
                "city": "Wellington",
                "country": "New Zealand",
                "population": 405000
              }
            ],
            [
              {
                "city": "Los Angeles",
                "country": "United States",
                "population": 3884307
              },
              {
                "city": "Berlin",
                "country": "Germany",
                "population": 3517424
              },
              {
                "city": "Jeddah",
                "country": "Saudi Arabia",
                "population": 3456259
              },
              {
                "city": "Kabul",
                "country": "Afghanistan",
                "population": 3414100
              },
              {
                "city": "Mashhad",
                "country": "Iran",
                "population": 3001184
              },
              {
                "city": "Milan",
                "country": "Italy",
                "population": 1359905
              },
              {
                "city": "Kiev",
                "country": "Ukraine",
                "population": 2908703
              },
              {
                "city": "Rome",
                "country": "Italy",
                "population": 2877215
              },
              {
                "city": "Chicago",
                "country": "United States",
                "population": 2695598
              },
              {
                "city": "Osaka",
                "country": "Japan",
                "population": 2691742
              }
            ],
            [
              {
                "city": "Bandung",
                "country": "Indonesia",
                "population": 2575478
              },
              {
                "city": "Managua",
                "country": "Nicaragua",
                "population": 2560789
              },
              {
                "city": "Paris",
                "country": "France",
                "population": 2229621
              },
              {
                "city": "Shiraz",
                "country": "Iran",
                "population": 1869001
              },
              {
                "city": "Manila",
                "country": "Philippines",
                "population": 1780148
              },
              {
                "city": "Montreal",
                "country": "Canada",
                "population": 1649519
              },
              {
                "city": "Guadalajara",
                "country": "Mexico",
                "population": 1495189
              },
              {
                "city": "Dallas",
                "country": "United States",
                "population": 1317929
              },
              {
                "city": "Yerevan",
                "country": "Armenia",
                "population": 1060138
              },
              {
                "city": "Tunis",
                "country": "Tunisia",
                "population": 1056247
              }
            ]
          ]
        },
        "meta": {
          "name": "Table"
        },
        "position": {
          "basis": "441px"
        },
        "propConfig": {
          "custom.chunk": {
            "binding": {
              "config": {
                "path": "../Dropdown_0.props.value"
              },
              "type": "property"
            }
          },
          "custom.data": {
            "binding": {
              "config": {
                "path": "this.props.data"
              },
              "enabled": false,
              "transforms": [
                {
                  "code": "\treturn list(lib.utils.chunks(value, 10))",
                  "type": "script"
                }
              ],
              "type": "property"
            }
          },
          "props.data": {
            "binding": {
              "config": {
                "expression": "{this.custom.data}[{this.custom.chunk}]"
              },
              "type": "expr"
            }
          },
          "props.selection.selectedRow": {
            "binding": {
              "config": {
                "expression": "len({this.props.data})-1"
              },
              "type": "expr"
            }
          }
        },
        "props": {
          "selection": {
            "data": [
              {
                "city": "Tunis",
                "country": "Tunisia",
                "population": 1056247
              }
            ]
          }
        },
        "type": "ia.display.table"
      }
    ],
    "meta": {
      "name": "root"
    },
    "props": {
      "direction": "column"
    },
    "type": "ia.container.flex"
  }
}

There are two phenomena in Perspective that contribute to the racy behavior you are experiencing.

  • Perspective property assignment is not synchronous. The following in a Perspective event yields surprising results at least some of the time:
	temporaryDS1 = system.dataset.toDataSet(headers, someNewRows)
	self.props.data = temporaryDS1
	temporaryDS2 = self.props.data
	system.perspective.print(str(temporaryDS1 is temporaryDS2))

A programmer could be forgiven for thinking a property that was just assigned a new value would have that new value on the next line of code, but in Perspective it might not. The above should print "True", but doesn't always.

  • Perspective component properties exist in two places, gateway and browser, and some of those always travel from gateway to browser (a table's props.data), and others travel only from browser to gateway (a table's props.selection.data). And some can be assigned from either direction (row and column selection).

With these in mind, you can imagine browser traffic scenarios where your row selection assignment, a fairly simple thing, makes it to the browser before your data is fully processed. Waiting a little bit can raise the odds of success, but doesn't guarantee it under heavy load.

The best results will happen if you use a property change on a browser=>gateway item that you know will change after props.data is processed at the browser. But you need a custom prop to act as marker when you update from the gateway side so the change script only acts when desired.

3 Likes

Don’t know that! Learn something new everyday.

2 Likes