Tomasz Narloch pisze:
Hello,

I have a problem with the transfer of variables config and app_globals to my own thread.
I need config and app_globals has been passed by reference.
Now, I like the one below, but it is not good because it throws a segmentation fault at the end of threads.

I read something about register config and agg_globals to threads in pylons but I do not know how to do that. #Maybe I should remove reference to config and app_globals after thread end?

I want the same config and app_globals in my thread and
when I change some params of app_globals (only one of three threads do that) it has to be changed in other pylons threads.

Maybe exists some other way to do that?

my thread class:
----------------------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
import threading

class Mythread(threading.Thread):
   # class variables, not instances only

   # Mythread.results[self.myId] = function()
   results = []
   vlock = threading.Lock()
   id = 0 # I want to give an ID number to each thread, starting at 0

   def __init__(self, function, **args):
       # invoke constructor of parent class
       threading.Thread.__init__(self)
       # add instance variables
       self.myId = Mythread.id
       Mythread.id += 1
       Mythread.results.append(None)
       self.function = function
       self.args = args
       self.args['vlock'] = Mythread.vlock

   def run(self):
       # update results in an atomic manner
       # nie ma wyścigu więc nie trzeba blokować
       #Mythread.vlock.acquire()
Uncommented
       Mythread.results[self.myId] = self.function(**self.args)
       #Mythread.vlock.release()
Uncommented

   @staticmethod
   def flush():
       Mythread.results = []
       Mythread.id = 0
#end
----------------------------------------------------------------------------------------------

I run thread from:
----------------------------------------------------------------------------------------------
thread.Mythread.flush()
   e0 = thread.Mythread(fun_1,  names=lnames1,   name=param)
   e1 = thread.Mythread(fun_2,  names=l.names2,  name=param)
   e2 = thread.Mythread(fun_3,  names=lnames3,   name=param)

   for s in [e0, e1, e2]:
       # I add additional param environ to function fun_1, fun_2, fun_3
       s.args['environ'] = pylons.request.environ
       s.start()

   # wait for all threads to completed
   for s in [e0, e1, e2]:
       s.join()

   nresult = thread.Mythread.results[-3]
   hresult = thread.Mythread.results[-2]
   eresult = thread.Mythread.results[-1]

----------------------------------------------------------------------------------------------

function as thread:

def fun_1(names, name, environ, vlock=None):
      some_file.config = environ['pylons.pylons'].config
replaced by

pylons.config._push_object(environ['pylons.pylons'].config)
pylons.app_globals._push_object(environ['pylons.pylons'].config['pylons.app_globals'])
try:
      root = some_file.expensiveFunctionThatNeedConfig(names)
      ....
finally:
pylons.app_globals._pop_object(environ['pylons.pylons'].config['pylons.app_globals'])
       pylons.config._pop_object(environ['pylons.pylons'].config)


----------------------------------------------------------------------------------------------

some_file content:

from pylons import config as conf, app_globals
replace by default
from pylons import config as config, app_globals
...
#saveThreadConfig, for now I can replace empty conf to that from environ
# in theads area conf is almost empty so I rewrite it, but this probably generate segmentation fault after threads end
config = conf
g = app_globals
...
removed above

def expensiveFunctionThatNeedConfig(names):
   # some work with config and g (app_globals)
   ...
----------------------------------------------------------------------------------------------

Best Regards,
Tomasz Narloch

After this changes works.

Best Regards,
Tomasz Narloch

--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to