免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1707 | 回复: 4
打印 上一主题 下一主题

python多进程问题求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-12 18:09 |只看该作者 |倒序浏览
代码如下,求高手指教

#!/usr/bin/env python
from multiprocessing import Manager
m = Manager()
d = m.dict()
d['a']=[]
d['a'].append(1)   这句无法执行,这个列表添加不了对象,不明白为什么,麻烦大侠指教

论坛徽章:
3
卯兔
日期:2013-08-15 13:17:31处女座
日期:2014-01-10 11:35:23双子座
日期:2014-01-25 02:16:06
2 [报告]
发表于 2013-06-12 18:18 |只看该作者
我这没有问题。。。

论坛徽章:
0
3 [报告]
发表于 2013-06-12 22:17 |只看该作者
sunny7476 发表于 2013-06-12 18:18
我这没有问题。。。


不能把,我打印出来的字典d发现这个值没有append进去啊

./t.py
{'a': []}

论坛徽章:
0
4 [报告]
发表于 2013-06-13 11:57 |只看该作者
回复 1# uselsee


dict()
dict(mapping)
dict(sequence)
Create a shared dict object and return a proxy for it.
list()
list(sequence)
Create a shared list object and return a proxy for it.
Note
Modifications to mutable values or items in dict and list proxies will not be propagated through the manager, because the proxy has no way of knowing when its values or items are modified. To modify such an item, you can re-assign the modified object to the container proxy:


# create a list proxy and append a mutable object (a dictionary)
lproxy = manager.list()
lproxy.append({})
# now mutate the dictionary
d = lproxy[0]
d['a'] = 1
d['b'] = 2
# at this point, the changes to d are not yet synced, but by
# reassigning the dictionary, the proxy is notified of the change
lproxy[0] = d

论坛徽章:
0
5 [报告]
发表于 2013-06-14 17:15 |只看该作者
回复 4# GhostFromHeaven

能帮我看看这个段代码为啥报错吗?

#!/usr/bin/env python
import paramiko, sys, multiprocessing, time
def sshcmd(myqueue):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    while True:
        ip = myqueue.get()
        ssh.connect(ip, username='root', password='xxxxxx')
        stdin, stdout, stderr = ssh.exec_command('ls -l')
        print stdout.read()
        myqueue.task_done()

if __name__  ==  '__main__':
    ips = ['10.69.12.19', '10.69.12.19']
    manager = multiprocessing.Manager()
    myqueue = manager.JoinableQueue()
    for i in ips:
        myqueue.put(i)

    for i in range(2):
        p = multiprocessing.Process(target = sshcmd, args = (myqueue,))
        p.daemon = True
        p.start()
    myqueue.join()
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP