Django 穿线有问题 include/machine.py views.py url.py

Django 穿线有问题 include/machine.py views.py url.py,django,multithreading,Django,Multithreading,当我访问时:发现所有机器信息func不执行,但我使用 #python manage.py shell >>> from birdman.views import all_machine_info >>> all_machine_info() 它确实执行,为什么?因为它与线程有关 include/machine.py 如果我使用find ssh.connect(ip,22,username='root',timeout=20,password=None),

当我访问时:发现所有机器信息func不执行,但我使用

#python manage.py shell
>>> from birdman.views import all_machine_info 
>>> all_machine_info()
它确实执行,为什么?因为它与线程有关


include/machine.py 如果我使用find ssh.connect(ip,22,username='root',timeout=20,password=None),它将不起作用,下面是我的日志文件:

2013-05-28 18:00:56,644 DEBUG starting thread (client mode): 0x16de7690L
2013-05-28 18:00:56,649 INFO Connected (version 2.0, client OpenSSH_4.3)
2013-05-28 18:00:56,650 DEBUG kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr'
, 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', '
arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac
-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'
] client lang:[''] server lang:[''] kex follows?False
2013-05-28 18:00:56,651 DEBUG Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
2013-05-28 18:00:56,651 DEBUG using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
2013-05-28 18:00:56,681 DEBUG Switch to new keys ...
2013-05-28 18:00:56,696 DEBUG Adding ssh-rsa host key for X.X.X.X
: 7679319647a2d5f231faf60391e37737
2013-05-28 18:00:56,697 DEBUG FUCK FUCK FUCK FUCK
2013-05-28 18:00:56,782 DEBUG EOF in transport thread
我使用#python machine.py,它是cat work,这里是paramiko.log:

DEB [20130528-18:03:24.031] thr=1   paramiko.transport: starting thread (client mode): 0x1258ec10L
INF [20130528-18:03:24.035] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_4.3)
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr',
'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'ae
s256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-rip
emd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:[
'none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, r
emote none
DEB [20130528-18:03:24.067] thr=1   paramiko.transport: Switch to new keys ...
DEB [20130528-18:03:24.082] thr=2   paramiko.transport: Adding ssh-rsa host key for X.X.X.X.: 4a5e4aafac28c0d035815c777956f4c8
DEB [20130528-18:03:24.095] thr=2   paramiko.transport: Trying discovered key 4a5344eded57cfbd2fecb1e6ac4f5f17 in /root/.ssh/id_rsa
DEB [20130528-18:03:24.107] thr=1   paramiko.transport: userauth is OK
INF [20130528-18:03:24.910] thr=1   paramiko.transport: Authentication (publickey) successful!
DEB [20130528-18:03:24.912] thr=2   paramiko.transport: [chan 1] Max packet in: 34816 bytes
DEB [20130528-18:03:24.912] thr=1   paramiko.transport: [chan 1] Max packet out: 32768 bytes
INF [20130528-18:03:24.913] thr=1   paramiko.transport: Secsh channel 1 opened.
DEB [20130528-18:03:24.915] thr=1   paramiko.transport: [chan 1] Sesch channel 1 request ok
DEB [20130528-18:03:24.955] thr=1   paramiko.transport: [chan 1] EOF received (1)
DEB [20130528-18:03:24.956] thr=2   paramiko.transport: [chan 2] Max packet in: 34816 bytes
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 1] EOF sent (1)
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 2] Max packet out: 32768 bytes
INF [20130528-18:03:24.957] thr=1   paramiko.transport: Secsh channel 2 opened.
DEB [20130528-18:03:24.998] thr=1   paramiko.transport: [chan 2] Sesch channel 2 request ok
DEB [20130528-18:03:25.038] thr=1   paramiko.transport: [chan 2] EOF received (2)
DEB [20130528-18:03:25.039] thr=2   paramiko.transport: [chan 3] Max packet in: 34816 bytes
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 2] EOF sent (2)
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 3] Max packet out: 32768 bytes
INF [20130528-18:03:25.040] thr=1   paramiko.transport: Secsh channel 3 opened.
DEB [20130528-18:03:25.081] thr=1   paramiko.transport: [chan 3] Sesch channel 3 request ok
DEB [20130528-18:03:25.122] thr=1   paramiko.transport: [chan 3] EOF received (3)
DEB [20130528-18:03:25.123] thr=2   paramiko.transport: [chan 4] Max packet in: 34816 bytes
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 3] EOF sent (3)
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 4] Max packet out: 32768 bytes
INF [20130528-18:03:25.123] thr=1   paramiko.transport: Secsh channel 4 opened.
DEB [20130528-18:03:25.165] thr=1   paramiko.transport: [chan 4] Sesch channel 4 request ok
DEB [20130528-18:03:25.206] thr=1   paramiko.transport: [chan 4] EOF received (4)

信任关系已经建立。

通过挖掘您的源代码,您似乎正在启动新线程,而实际上并不等待它们完成。试试这样的

def all_machine_info():
    f = open(server_list)
    lines = f.readlines()

    jobs = [] # List of background jobs

    for ip in lines:
        a = threading.Thread(target = machine_info,args = (ip,))
        a.start()
        jobs.append(a)

    for a in jobs:
        a.join() # Wait for job to finish
    f.close()

尝试将代码示例减少到不起作用的绝对最小值(即,删除起作用的代码,以便我们可以关注您的问题)。这很有用,但我还有一个问题。
def cpu_info(ip):
        #paramiko.util.log_to_file('paramiko.log')
        cpuinfo = []
        cmd_list = ["cat /proc/cpuinfo |grep 'processor' |wc -l","cat /proc/cpuinfo |grep 'physical id' |sort |uniq |wc -l","cat /proc/cpuinfo |grep 'core id' |sort |uniq |wc -l","awk -F: '/model name/ {print $2}' /proc/cpuinfo |t
ail -n 1"]
        try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(ip,22,username='root',timeout=20,password=None)
                for cmd in cmd_list:
                        stdin,stdout,stderr = ssh.exec_command(cmd)
                        cpuinfo.append(stdout.read().strip())
                ssh.close()
                return cpuinfo
        except:
                msg = 'When collect cpuinfo Connection Error %s ' % ip
                logging.debug('FUCK FUCK FUCK FUCK')
                print msg
2013-05-28 18:00:56,644 DEBUG starting thread (client mode): 0x16de7690L
2013-05-28 18:00:56,649 INFO Connected (version 2.0, client OpenSSH_4.3)
2013-05-28 18:00:56,650 DEBUG kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr'
, 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', '
arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac
-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'
] client lang:[''] server lang:[''] kex follows?False
2013-05-28 18:00:56,651 DEBUG Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
2013-05-28 18:00:56,651 DEBUG using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
2013-05-28 18:00:56,681 DEBUG Switch to new keys ...
2013-05-28 18:00:56,696 DEBUG Adding ssh-rsa host key for X.X.X.X
: 7679319647a2d5f231faf60391e37737
2013-05-28 18:00:56,697 DEBUG FUCK FUCK FUCK FUCK
2013-05-28 18:00:56,782 DEBUG EOF in transport thread
DEB [20130528-18:03:24.031] thr=1   paramiko.transport: starting thread (client mode): 0x1258ec10L
INF [20130528-18:03:24.035] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_4.3)
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-ctr',
'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] server encrypt:['aes128-ctr', 'aes192-ctr', 'ae
s256-ctr', 'arcfour256', 'arcfour128', 'aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', 'arcfour', 'rijndael-cbc@lysator.liu.se'] client mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-rip
emd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:[
'none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEB [20130528-18:03:24.037] thr=1   paramiko.transport: using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, r
emote none
DEB [20130528-18:03:24.067] thr=1   paramiko.transport: Switch to new keys ...
DEB [20130528-18:03:24.082] thr=2   paramiko.transport: Adding ssh-rsa host key for X.X.X.X.: 4a5e4aafac28c0d035815c777956f4c8
DEB [20130528-18:03:24.095] thr=2   paramiko.transport: Trying discovered key 4a5344eded57cfbd2fecb1e6ac4f5f17 in /root/.ssh/id_rsa
DEB [20130528-18:03:24.107] thr=1   paramiko.transport: userauth is OK
INF [20130528-18:03:24.910] thr=1   paramiko.transport: Authentication (publickey) successful!
DEB [20130528-18:03:24.912] thr=2   paramiko.transport: [chan 1] Max packet in: 34816 bytes
DEB [20130528-18:03:24.912] thr=1   paramiko.transport: [chan 1] Max packet out: 32768 bytes
INF [20130528-18:03:24.913] thr=1   paramiko.transport: Secsh channel 1 opened.
DEB [20130528-18:03:24.915] thr=1   paramiko.transport: [chan 1] Sesch channel 1 request ok
DEB [20130528-18:03:24.955] thr=1   paramiko.transport: [chan 1] EOF received (1)
DEB [20130528-18:03:24.956] thr=2   paramiko.transport: [chan 2] Max packet in: 34816 bytes
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 1] EOF sent (1)
DEB [20130528-18:03:24.956] thr=1   paramiko.transport: [chan 2] Max packet out: 32768 bytes
INF [20130528-18:03:24.957] thr=1   paramiko.transport: Secsh channel 2 opened.
DEB [20130528-18:03:24.998] thr=1   paramiko.transport: [chan 2] Sesch channel 2 request ok
DEB [20130528-18:03:25.038] thr=1   paramiko.transport: [chan 2] EOF received (2)
DEB [20130528-18:03:25.039] thr=2   paramiko.transport: [chan 3] Max packet in: 34816 bytes
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 2] EOF sent (2)
DEB [20130528-18:03:25.039] thr=1   paramiko.transport: [chan 3] Max packet out: 32768 bytes
INF [20130528-18:03:25.040] thr=1   paramiko.transport: Secsh channel 3 opened.
DEB [20130528-18:03:25.081] thr=1   paramiko.transport: [chan 3] Sesch channel 3 request ok
DEB [20130528-18:03:25.122] thr=1   paramiko.transport: [chan 3] EOF received (3)
DEB [20130528-18:03:25.123] thr=2   paramiko.transport: [chan 4] Max packet in: 34816 bytes
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 3] EOF sent (3)
DEB [20130528-18:03:25.123] thr=1   paramiko.transport: [chan 4] Max packet out: 32768 bytes
INF [20130528-18:03:25.123] thr=1   paramiko.transport: Secsh channel 4 opened.
DEB [20130528-18:03:25.165] thr=1   paramiko.transport: [chan 4] Sesch channel 4 request ok
DEB [20130528-18:03:25.206] thr=1   paramiko.transport: [chan 4] EOF received (4)
def all_machine_info():
    f = open(server_list)
    lines = f.readlines()

    jobs = [] # List of background jobs

    for ip in lines:
        a = threading.Thread(target = machine_info,args = (ip,))
        a.start()
        jobs.append(a)

    for a in jobs:
        a.join() # Wait for job to finish
    f.close()