It might help if you provide a little more detail regarding what D is and 
what kind of processing is going on. Why do you create D in the controller 
but newD in the view? Does D get used anywhere before newD is created? 
Could you just create newD in the controller as well? Is D/newD unique per 
user/session, or is it the same across all sessions?

As it is, it looks like you could get a race condition. An initial request 
to func1 leaves newD in my_pckl.p. Then before the first Ajax request to 
func2, a second request to func1 overwrites my_pckl.p with D, and then the 
first Ajax request to func2 reads D out of my_pckl.p before the view 
changes it to newD (i.e., so the Ajax request gets D instead of newD). The 
problem is worse if the value of newD depends on the user/session -- since 
you always use the same filename, the newD for one user could be 
overwritten by a func1 request from another user before the first user's 
func2 Ajax request. The whole process will be slowed down a bit by the file 
writing/reading as well.

If newD is the same for all users, you might store it in cache.ram rather 
than writing it to a file -- that will be faster and you won't have to 
worry about doing the pickling yourself. If newD is specific to each user, 
then just store it in the session (again, you won't have to pickle it 
yourself).

Anthony

On Saturday, February 11, 2012 4:31:08 AM UTC-5, Vineet wrote:
>
> From View, I am calling server side controller function via web2py's 
> ajax function with ':eval' 
> So far, so good. 
>
> Further I need to return a dict from this ajax controller (in addition 
> to "return js"), 
> Then access this dict in View & process it. 
> Finally, return dict to another controller for server side processing. 
>
> ++++++++++++++++++++++++++++++++++++++ 
> I am doing it like this. 
>
> def func1(): 
>     D = ({1:'a', 2:'b'}, {3:'c', 4:'d'}) 
>     cPickle.dump(D, open( "my_pkcl.p", "wb")) 
>     return dict() 
>
> This D is accessed in 'View' and processed. 
>
> #### (Why I am not using   return dict(D=D)? 
> #### It is explained at the end.) 
>
> {{newD = cPickle.load(open( "my_pkcl.p", "rb" ))}} 
> {{for i in newD:}} 
> .......processing statements....... 
> {{pass}} 
>
> After processing newD, it is again pickled with same filename. 
> {{cPickle.dump(newD, open( "my_pkcl.p", "wb" ) )}} 
>
> Then ajax function is called-- 
> ajax("{{=URL('func2)}}", [], ":eval"); 
>
> def func2(): 
> # access the D which has been changed by 'View' code 
>     changedD = cPickle.load( open( "my_pkcl.p", "rb" ) ) 
>     ... 
>     ... 
>     process changedD as per requirement 
>
> ++++++++++++++++++++++++++++++++++++++ 
> Why I am not using return dict(D=D)? 
> While calling 'func2' via web2py's ajax function, I can't pass the 
> Dict (or any other python object) as argument or vars. 
> So I resorted to cPickle it. 
> +++++++++++++++++++ 
> My question is:-- 
> I am cPickling in controller & loading (unpickling) in View. 
> After processing in view, cPickling it & again loading (unpickling) in 
> controller. 
>
> Is it OK or is it weired / horrible (considering speed & good coding 
> practice) ? 
> If not OK, how do I do it with any alternative method ? 
>
> Thanks, 
>
> Vineet

Reply via email to