On 08/30/2012 02:32 AM, John Lee wrote:
Dear all,

I was trying to debug a multi-thead issue. In order to reproduce it, I
wrote a multi-thread testing in tests.py, but got exception in
get_renderer('xxxx.pt'). After some digging, it turns out get_renderer
will eventually call get_current_registry, which is thread local.  So, I
came out with this hack:

<pre>
import pyramid.threadlocal
from threading import Thread, Lock

candidates = [
     (self._test1, ()),
     (self._test2, ()),
     (self._test3, ()),
     ]

def random_func(pyramid_thread_locals):
     pyramid.threadlocal.manager.push(pyramid_thread_locals)
     time.sleep(random.random())  # 0 ~ 1 sec
     func, args = random.choice(candidates)
     func(*args)

pyramid_thread_locals = pyramid.threadlocal.manager.get()
threads = [Thread(target=random_func, args=(pyramid_thread_locals, ),)
            for i in range(100)]
for thread in threads:
     thread.start()
for thread in threads:
     thread.join()
</pre>

There is no guarantee that pyramid.threadlocal.manager will always be
there. Even if it's there, there's no guarantee it can be used this way.
So, this should only be considered as a temporary workaround.

Question: is there a better way to do this?

Is there a better way to do what? What problem are you trying to solve? Is the above code a bug report?

- C

--
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