Jump to content

Are modules thread safe?


Nils-H

Recommended Posts

Having a Java background, thread safety is something I usually consider with my code. So I'm wondering how this works with PrestaShop/PHP. Are modules thread safe? That is, will each request have a new instance of the module, so I can safely use instance variables to keep state through a request? Or should I not keep any state in module instance variables?

Link to comment
Share on other sites

They are thread safe since 1.4, but earlier versions were not. But overall thread safety is rarely causes any issues in PHP and prestashop, as each request to the server is contained within a single thread. You need to think in terms of requests and sessions instead of threads.

Modules in Prestashop instantiated trough the module class getinstancebyname method. In the current version the same instance would be used as long as this method is used, so you can use module variables in different parts of your code. But if you are too converned you can always use static variables.

Link to comment
Share on other sites

Yes, I realize that this is dependent on server configuration, but I was thinking generally, and in the case where threading is enabled. Although thread safety was maybe not what I was looking for anyway, but rather if there was a shared instance of a module in the runtime, or if it is created per request. If it is created per request, then using instance variables would be safe, so if that's the case, then everything is ok.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...