CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sun Jun 16, 2019 8:03 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Python and threads
PostPosted: Wed Apr 11, 2012 12:44 pm 
Offline

Joined: Wed Apr 11, 2012 9:54 am
Posts: 13
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


Top
   
 Post subject: Re: Python and threads
PostPosted: Wed Apr 11, 2012 1:50 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, Matthias,

Matthias S. wrote:
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. wrote:
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. wrote:
- 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. wrote:
- 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!

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
 Post subject: Re: Python and threads
PostPosted: Thu Apr 12, 2012 2:20 pm 
Offline

Joined: Wed Apr 11, 2012 9:54 am
Posts: 13
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


Top
   
 Post subject: Re: Python and threads
PostPosted: Tue Jun 05, 2012 6:46 am 
Offline

Joined: Wed Apr 11, 2012 9:54 am
Posts: 13
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


Top
   
 Post subject: Re: Python and threads
PostPosted: Tue Jun 05, 2012 12:55 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, Matthias,

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

Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited