Pythonã®ã¹ã³ã¼ãã§åã³è¿·åã
id:mopemopeæ°ã¨ãid:jbkingæ°ã«åªããçªã£è¾¼ã¾ããã°ãã¼ãã«å¤æ°ãªãã¦ä½¿ãã®ãããã¨æããã¯ã©ã¹ãä½ã£ã¦ã¡ã³ãå¤æ°ã§å¤æããã¦ã¿ã¾ããã使ãã¨ãã¯ãã²ãã¿ã¨ã»ãã¿ã§ããããåºãå ¥ããããã¨ã«ãã¾ããã
ããã§åã³è¿·åããã£ããã¨ã¯ã
å¤æ°ã¯ãGDataã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹gDataã«æãããã
ããããModMainã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹modMainããå©ç¨ããã
ãã ããmodMainã¯ãåããã»ã¹ã§åããã
modMainãstart()ããã¦ããã°ãããã£ã¦ãããmodMainã®å¤ããgDataã®ã»ãã¿ã¼ã§ä¸èº«ãæ¸ãæãã¦ã¿ãã¨ã»ã»ã»
modMainã®gDataã«ã¯ä½ã®å½±é¿ãåã¼ããªããï¼
modMainèµ·åããã¨ãã«gDataã渡ãã¦ãªããããmodMainã¯ç©ºæ°ãèªãã§gDataããå¤ãåã£ã¦ãã¦ãã¨æã£ããã©ãããããæåã«ãªã£ã¦ãªãã¨è¨ããã¨ã¯ã.start()ããã¨ãã®ãèªåã¨ãã®ã¨ãã«åå¨ããã¤ã³ã¹ã¿ã³ã¹ã®æ å ±ããã¹ã¦ã¾ãã£ã¨ãèªåã®ç©ºéã«ã³ãã¼ãã¦ãã¾ãã®ããªï¼ããããã ã£ãããã ããã.start()ããã¨ãã«ãå ¨ãé¢ä¿ãªãè¶ å·¨å¤§ãªã¤ã³ã¹ã¿ã³ã¹ãåå¨ãããããã®åãã«ãã«ã¨ã¡ã¢ãªãåã ã«ãªã£ã¦ããã¨ãããã¨ãï¼ï¼
ãã¾ãã¡æåãèªããªãã»ã»
以ä¸ããã¹ãã«ä½¿ã£ãã³ã¼ã
#!/usr/bin/env python # coding:utf-8 from multiprocessing import Process from time import sleep class GData(): # ãã®ããã°ã©ã ã§ä½¿ãå¤æ°ãåãæã¤ã¯ã©ã¹ def __init__(self): self.a = 100 self.b = 'hoge' def getData(self): return (self.a, self.b) def setData(self, a=None, b=None): self.a = a self.b = b class ModMain(): # mainã§ã°ã«ã°ã«åãå¦çããå def __init__(self): self.cnt = 20 def run(self): while self.cnt: print gData.getData() self.cnt += -1 sleep(0.2) gData = GData() modMain = ModMain() Process(target = modMain.run).start() sleep(1) gData.setData(a=1, b='foo') print '-' * 80 print gData.getData() print '-' * 80
çµæ
(100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') -------------------------------------------------------------------------------- (1, 'foo') -------------------------------------------------------------------------------- (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge') (100, 'hoge')
ãã¡
æ©ã¿ã¯å°½ããã»ã»ã»