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?

Regards,
John

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/pylons-discuss/-/uswP7wu1QbEJ.
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