Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Python and threads

Anonymous
2012-04-11
2012-06-05
  • Anonymous - 2012-04-11

    Originally created by: Matthias S.

    Hello Forum,

    I want to use Python to control and monitor a running CoDeSys PLC.

    The tasks of the Python script will be:
    - Read periodically variables from the PLC
    - Sending e-mails if a particular value occurs
    - Provide a simple HTTP Server to access from remote the variables

    I’ve written a small test-Python-script in CoDeSys with two threads - It works without problems.

    My questions are:
    - Is multithreading explicitly supported in CoDeSys-Python
    - I use 2 Python threads which print a debug value. This caused a crash in CoDeSys.
    It seems that the β€žprintβ€œ instruction is not thread safe.
    - Which instructions and which CoDeSys Python extensions are thread safe in CoDeSys?

    Thank you in advance!

    Greetings,
    Matthias

     
  • Anonymous - 2012-04-11

    Originally created by: M.Schaber

    Hi, Matthias,

    Matthias S. hat geschrieben:
    I want to use Python to control and monitor a running CoDeSys PLC.
    The tasks of the Python script will be:
    - Read periodically variables from the PLC
    - Sending e-mails if a particular value occurs
    - Provide a simple HTTP Server to access from remote the variables

    This sounds like one of the most advanced usages for CoDeSys Scripting I heard of up to now. I'm interested to hear more about your mileage. Which server library are you using?

    Matthias S. hat geschrieben:
    I’ve written a small test-Python-script in CoDeSys with two threads - It works without problems.
    My questions are:
    - Is multithreading explicitly supported in CoDeSys-Python

    It is possible to use threads in Python scripts - both the one provided by the python threading module, as well as the ones provided by the .NET System.Threading namespace. Mixing both approaches in the same script may be problematic, as far as IronPython itsself may have some problems in that area.

    But while we do not prevent advanced script authors from using threads, we do not officially or explicitly support threading, it is "on your own risk".

    Matthias S. hat geschrieben:
    - I use 2 Python threads which print a debug value. This caused a crash in CoDeSys.
    It seems that the β€žprintβ€œ instruction is not thread safe.

    Most internal APIs in CoDeSys itsself are not thread safe, and so are the scripting APIs which rely on them - they are only safe to be called from the primary thread. "print" for example is redirected to the IMessageStorage service, which involves UI operations (updating the message window in CoDeSys) and thus can only be driven from the primary thread.

    Matthias S. hat geschrieben:
    - Which instructions and which CoDeSys Python extensions are thread safe in CoDeSys?

    For the IronPython builtin functions, and the ones provided by the (Iron)Python standard library, see their documentation about thread safety.

    For the objects provided by the CoDeSys ScriptDrivers, only the ones which are explicitly documented so are thread safe - currently, this are very few, if any. All other functionality is only available from the primary thread.

    In V3.5 SP1, the system object gets a execute_on_primary_thread method which allows scheduling of code back to the primary thread. In the meantime, you need to use other mechanisms as provided by the aforementioned threading libraries for that functionality. Also you should be aware that when the main thread returns from the script execution, some temporary state and context is cleaned up, so you should take care that you keep the main thread running and processing the message loop as long as your script is running.

    I think you could success using the following ideas:
    - The primary thread periodically reads the values of the monitored variables into some python variables, and just waits the rest of the time.
    - Use your own threads to serve the requests by the HTTP server and send the emails.
    - Use proper synchronization for the storage (IronPython itsself needs more careful synchronization compared to cPython, as there is no global interpreter lock).

    Good Luck!

     
  • Anonymous - 2012-04-12

    Originally created by: Matthias S.

    Hello M. Schaber,

    thank you very much for your help.

    Your recommendation with a main thread for data montoring and another for the other stuff works fine.
    I wrote a small test script to test some features and concepts.

    I will develop the "real" Python script with the full functionality in the next weeks.
    As soon as I make progress I write it here again.

    It's a great idea to support Python in CoDeSys.

    Greetings,
    Matthias

     
  • Anonymous - 2012-06-05

    Originally created by: Matthias S.

    Hello,

    The project is progressing slowly.
    I compared several web-frameworks for python and decided the "Bottle" Web Framework.

    http://bottlepy.org/docs/dev/
    This framework has no dependencies other than the Python Standard Library.
    It consists of only 1 source file.

    Until now it works without problems with the CoDeSys Script Engine.

    Thanks,
    Matthias

     
  • Anonymous - 2012-06-05

    Originally created by: M.Schaber

    Hi, Matthias,

    I'm happy to hear that you're making good progress here.

    Markus

     

Log in to post a comment.