Skip to content

Instantly share code, notes, and snippets.

@salexkidd
Created November 14, 2012 17:58
Show Gist options
  • Save salexkidd/4073682 to your computer and use it in GitHub Desktop.
Save salexkidd/4073682 to your computer and use it in GitHub Desktop.
#!/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()
@salexkidd
Copy link
Author

なんで wrong_queue_register_method() の方では、マネージャに登録したオブジェクトが全部同じものになってしまうん???

@salexkidd
Copy link
Author

一応、ダメコードで別のオブジェクトになったんだけど、、だせぇ!

( ◠‿◠ )☛それとコード中の英語は全部適当だ
▂▅▇█▓▒░(’ω’)░▒▓█▇▅▂うわああああああ

@mactkg
Copy link

mactkg commented Nov 15, 2012

▂▅▇█▓▒░(’ω’)░▒▓█▇▅▂うわああああああ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment