Created
November 14, 2012 17:58
-
-
Save salexkidd/4073682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
Problem of Resigter queue to Manager. | |
""" | |
from multiprocessing import managers, Queue | |
class WrongManager(managers.SyncManager): | |
pass | |
class CorrectManager(managers.SyncManager): | |
pass | |
def wrong_queue_register_method(): | |
#register different queue to TestManager class | |
for queue_name in ("queue_a", "queue_b", "queue_c"): | |
q = Queue() | |
WrongManager.register(queue_name, callable=lambda: q) | |
#create Manager object | |
mgr = WrongManager( | |
address=("127.0.0.1", 10000), | |
authkey="abc", | |
) | |
mgr.start() | |
print "Wtf!!! Why all queue is same object !?" | |
for queue_name in ("queue_a", "queue_b", "queue_c",): | |
queue_func = getattr(mgr, queue_name) | |
print "object_id:{}\tqueue:{}".format( | |
id(queue_func()), | |
queue_func() | |
) | |
mgr.shutdown() | |
def correct_queue_register_method(): | |
from random import choice | |
import string | |
for queue_name in ("queue_a", "queue_b", "queue_c",): | |
#make random variable | |
random_name = \ | |
"".join([choice(string.lowercase) for i in range(15)]) | |
#put queue to random variable | |
globals()[random_name] = Queue() | |
#make register python code | |
register_str = "CorrectManager.register(queue_name,"\ | |
"callable=lambda: {r})" | |
exec(register_str).format(r=random_name) | |
#create Manager object | |
mgr = CorrectManager( | |
address=("127.0.0.1", 20000), | |
authkey="abc", | |
) | |
mgr.start() | |
print "Yes! Register different queue to manager!!!" | |
for queue_name in ("queue_a", "queue_b", "queue_c"): | |
queue_func = getattr(mgr, queue_name) | |
print "object_id:{}\tqueue:{}".format( | |
id(queue_func()), | |
queue_func() | |
) | |
mgr.shutdown() | |
if __name__ == '__main__': | |
print "\n" + "-*" * 30 | |
wrong_queue_register_method() | |
print "\n" + "-*" * 30 | |
correct_queue_register_method() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一応、ダメコードで別のオブジェクトになったんだけど、、だせぇ!
( ◠‿◠ )☛それとコード中の英語は全部適当だ
▂▅▇█▓▒░(’ω’)░▒▓█▇▅▂うわああああああ