<div dir="ltr">Excellent. As always, thanks a lot Oscar for your dedication and time to help us with these questions.<div>We will keep an eye on that and avoid sharing stores between threads.</div><div><br></div><div>Kind regards</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-08-05 8:21 GMT-06:00 Oscar Campos <span dir="ltr"><<a href="mailto:oscar.campos@member.fsf.org" target="_blank">oscar.campos@member.fsf.org</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">It looks good.</p>
<p dir="ltr">It will work as far as you don't try to share stores or storm objects between threads.</p><div class="HOEnZb"><div class="h5">
<div class="gmail_quote">On 5 Aug 2014 15:18, "R Pish" <<a href="mailto:rpishcr@gmail.com" target="_blank">rpishcr@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div>Thank you so much for your detailed explanation sir!<br></div><div><br>As a matter of fact, we have some helper functions as the "read_user" function you posted that need a store to retrieve data. What we are currently doing is to have a required parameter in that function which is the existing store to use to prevent creating a new store inside the function. Something like:<br>


<br><div style="font-family:Helvetica,Arial;font-size:13px;color:rgb(0,0,0);margin:0px">def read_user(store, user_id):</div>    user = store.get(User, user_id)<div style="font-family:Helvetica,Arial;font-size:13px;color:rgb(0,0,0);margin:0px">


    return user</div><br><div>from models.meta import zstorm</div><div>class LoginResource:</div><div>    # POST /login</div><div>    def on_post(self, req, resp):</div><div>        store = zstorm.get("store_provider")</div>



<div>        user = read_user(store, 25)<br></div><div>...</div><br></div><div>So, ideally we would have only 1 store created per request (the one created at the request handler of the API).<br><br></div><div>How does the above look like?<br>


<br></div><div>Thanks again,<br></div><div>Regards<br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-08-05 4:20 GMT-06:00 Oscar Campos <span dir="ltr"><<a href="mailto:oscar.campos@member.fsf.org" target="_blank">oscar.campos@member.fsf.org</a>></span>:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


Hi.</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


The design that you shown here will work but there is some considerations that maybe you want to have into account.</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


<br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">ZStorm doesn’t make your stores non-blocking, it doesn’t works on that way. ZStorm guarantee that if you call it in any thread, it will returns you a valid store for that thread always. This works much better in an scenario where you have a thread pool available.</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


Take into account that you can never used any object that has been returned from a thread in another thread (not even in the min thread) so if you have a function that runs in another execution thread and do something like:</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


def read_user(user_id):</div><div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">    store = zstorm.get(‘store_provider’)</div></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


    user = store.get(User, user_id)</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">    return user</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


<br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">It will not work as Storm lazy evaluate object properties, that means that Storm may or may not hit the database to retrieve the user information in the previous function so the next time that you try to access a property in the object for example:</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


user = read_user(1263)</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">print(<a href="http://user.name" target="_blank">user.name</a>)</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


<br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">This will raise an exception in the ZStore module as Storm will try to hit the database in order to retrieve the information for the user and is going to try to use the store that is attached to the object itself that has been created in another execution thread and can not be used in any other thread.</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


Another thing to have into consideration is that if you use a database engine that supports transactions like InnoDB or just Postgres (in case that you use Postgres instead of MySQL) you have to make sure to commit your transactions (even for selects) as each store in each thread will have their own idea of what is the database state and will not be aware of changes made from other stores until both stores are committed.</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


Other challenge that you may to archive is serialise access to data that can be suffering of any type of race condition problem, for example, update the cash of a customer. The best way to do so is using some kind of trigger action in the database.</div>


<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">


Regards.</div><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div> <div><div style="font-family:helvetica,arial;font-size:13px">-- </div><div style="font-family:helvetica,arial;font-size:13px">


Join the free software foundation and become free as in freedom<br></div></div><div><div> <br><p style="color:#000">On 5 August 2014 at 05:44:03, R Pish (<a href="mailto:rpishcr@gmail.com" target="_blank">rpishcr@gmail.com</a>) wrote:</p>


 </div></div><blockquote type="cite"><span><div><div></div><div><div><div>






<div dir="ltr">
<div>So, we started using storm recently and we are still in a
development process, but before going any further, we want to know
if we are making things right.</div>
<div><br></div>
<div>The application being built is a REST API developed using
Falcon Framework.</div>
<div><br></div>
<div>When the application starts, we import a file (models.meta)
which has the following code</div>
<div><br></div>
<div>...</div>
<div>database_dsn = "%s://%s:%s@%s:%s/%s" % (db_driver, db_user,
db_pwd, db_host, db_port, db_name)</div>
<div>from zope.component import provideUtility, getUtility</div>
<div>from storm.zope.interfaces import IZStorm</div>
<div>from storm.zope.zstorm import global_zstorm</div>
<div>provideUtility(global_zstorm, IZStorm)</div>
<div>zstorm = getUtility(IZStorm)</div>
<div>zstorm.set_default_uri("store_provider", database_dsn)</div>
<div><br></div>
<div>And, in the file that contains the class that handles the
calls to a particular entity in the REST API has the following
(abbreviated):</div>
<div><br></div>
<div>from models.meta import zstorm</div>
<div>class LoginResource:</div>
<div>    # POST /login</div>
<div>    def on_post(self, req, resp):</div>
<div>        store =
zstorm.get("store_provider")</div>
<div>        user = store.find...</div>
<div>...</div>
<div><br></div>
<div>So, the code above is the basic idea we are currently starting
to use.<br></div>
<div>I wanted to know if just by doing: store =
zstorm.get("store_provider") in each function that handles a
particular request, is enough to guarantee that the app will be
thread-safe and, for example, that 2 concurrent requests will get
their non-blocking stores and use the database without blocking
each other? (storm does this internally and transparently for the
developer?)</div>
<div><br></div>
<div>
<div>I read the following in Storm-Manual:</div>
<div>"Therefore, the best policy is usually to create a Store
object for each thread which needs one. One convenient way to
manage this is to implement a "threadsafe" Store manager, which
creates a Store for each thread when that thread first requests
one, and then keeps the Store cached in a thread-local dictionary.
This approach is taken by ZStorm to provide Store objects to each
thread."</div>
<div><br></div>
</div>
<div>If our idea above is wrong, what would we could do to make our
app thread-safe?</div>
<div><br></div>
<div>Thanks a lot in advance for any help</div>
<div>Regards.</div>
</div></div></div><span><font color="#888888">


--  
<br>storm mailing list
<br><a href="mailto:storm@lists.canonical.com" target="_blank">storm@lists.canonical.com</a>
<br>Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/storm" target="_blank">https://lists.ubuntu.com/mailman/listinfo/storm</a>
<br></font></span></div></div></span></blockquote></div></blockquote></div><br></div>
</blockquote></div>
</div></div></blockquote></div><br></div>