Take a look at the jython/BackAndForth.py sample script. (In the JMRI distribution or here: )
It creates an indepedent thread via the AbstractAutomaton class, which handles those details. That then waits for sensors and commands a throttle.
For more:
Bob
On Jan 30, 2020, at 1:16 AM, James Anderson <james_anderson_999@...> wrote:
Hi, I have included a small cut down version of something I am trying to do.
I set up a propertyChangeLlistener for a sensor. When the sensor is activated it runs. I have simulated it running a long time just by putting a time.sleep in.
My problem is that when the se.setSate(ACTIVE) in the main function runs. It does not complete until the propertChangeListener completes.
Is there a way to have the propertyChageListener run without the main function waiting for it to complete.
I have looked at some of the documentation on threads but do not understand how to get this to work with jython.
I have used threading.Thread from the standard library start a thread running to do the long running task but this has some really strange behaviour and does not appear to run as thread but runs sequentially.
Some examples of why I want to do this is that when a sensor is activated I want to set a function key on the throttle a number of seconds later and not at the time the senor is activated or to start running some animation program on the layout that
could take some time.
Thanks jim.
Code
---------
import jmri
import java.beans
import time
class _SET_SENSOR(jmri.jmrit.automat.AbstractAutomaton) :
def init(self):
return
def handle(slef):
se=sensors.provideSensor("IStest")
se.addPropertyChangeListener(_LISTENER(), "IStest", "TCPDeviceMgr")
print(str(time.ctime())+" setting ACTIVE")
se.setState(ACTIVE)
print(str(time.ctime())+" setting INACTIVE")
se.setState(INACTIVE)
return
class _LISTENER(java.beans.PropertyChangeListener):
def propertyChange(self, event):
if event.newValue==2:
print(str(time.ctime())+" PropertyChange Activated...")
se=sensors.getSensor("IStest")
for oldListener in se.getPropertyChangeListenersByReference("IStest"):
if se.getListenerRef(oldListener) == "TCPDeviceMgr":
print(str(time.ctime())+" Removing old listner for sensor : IStest")
se.removePropertyChangeListener(oldListener)
time.sleep(15)
print(str(time.ctime())+" PropertyChange Complete")
return
test=_SET_SENSOR()
test.start()
OUTPUT from script
---------------------------
Thu Jan 30 08:48:24 2020 setting ACTIVE
Thu Jan 30 08:48:24 2020 PropertyChange Activated...
Thu Jan 30 08:48:24 2020 Removing old listner for sensor : IStest
--->>> 15 second gap for the time.sleep(15) inside the propertyChangeListener function.
Thu Jan 30 08:48:39 2020 PropertyChange Complete
Thu Jan 30 08:48:39 2020 setting INACTIVE
--
Bob Jacobsen
rgj1927@...