2012-08-04 104 views
0

我已經創建了一個小型python腳本,用於在ubuntu 12.04盒子上使用uTorrent下載torrent時發送通知。當我運行pgrep -l洪流時,我可以看到scipt的負載並且無法殺死它們。 utorrent調用傳遞torrent名稱的腳本。腳本工作正常,從終端運行時退出,但在utorrent調用它時不會關閉。我嘗試將sys.exit()添加到腳本的底部,但它並未停止該進程並停止發送通知。Python uTorrent通知腳本不會終止

我能做些什麼來使scipt關閉?還有任何想法如何我可以殺死這些進程?我試過殺pkill和htop。

謝謝。

#!/usr/bin/python 

import sys 
import os 
import socket 

def network_notify(message): 
    host = ['192.168.0.4', '192.168.0.6'] #server IP 
    port = 50000 
    size = 1024 
    title = 'Torrent Downloaded' 

    for i in host: 
     try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      s.connect((i,port)) 
     except: 
      None 
     else: 
      s.send('\"' + title + '\" \"' +message+'"') 
      data = s.recv(size) 
      s.close() 

if len(sys.argv) > 1: 
    name = ' '.join(sys.argv[1:]) 
    network_notify(name) 

繼承人和pgrep

[email protected]$ pgrep -l torrent 
27516 torrentNotify.p 
27518 torrentNotify.p 
27520 torrentNotify.p 
27521 torrentNotify.p 
27522 torrentNotify.p 
27529 torrentNotify.p 
27531 torrentNotify.p 
27540 torrentNotify.p 
27541 torrentNotify.p 
27545 torrentNotify.p 
27546 torrentNotify.p 
27550 torrentNotify.p 
27551 torrentNotify.p 
27552 torrentNotify.p 
27553 torrentNotify.p 
27555 torrentNotify.p 
27558 torrentNotify.p 
27567 torrentNotify.p 
27570 torrentNotify.p 
27571 torrentNotify.p 
27573 torrentNotify.p 
27574 torrentNotify.p 
28959 torrentNotify.p 
28965 torrentNotify.p 
28970 torrentNotify.p 
+0

什麼是這些「wchan」? – 2012-08-04 01:39:38

+0

wchan對他們全部是退出 – James 2012-08-04 02:39:43

+1

你的所有''除:'子句可能隱藏了一些有用的信息。而且,其中一個'socket'方法調用可能被掛起。可能需要添加一些日誌記錄來調試哪一個。 – martineau 2012-08-04 02:54:55

回答

1

嘗試了這一點,當你運行該腳本確保當它被稱爲你的uTorrent調試儲蓄做/path/to/script.py >> debug.txt。這應該會給你更好的錯誤處理,並在完成時顯式關閉套接字。我也添加了一個小暫停。

我猜套接字是掛在腳本的一部分。調用關機會停止所有流量,然後關閉將關閉套接字。

#!/usr/bin/python 

import logging 
import os 
import socket 
import sys 

def network_notify(name): 
    hosts = ['192.168.0.4', '192.168.0.6'] #server IP 
    port = 50000 
    size = 1024 
    title = 'Torrent Downloaded' 

    for host in hosts: 
     try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      # Set the socket to timeout after 10 seconds 
      s.settimeout(10.0) 
      s.connect((host, port)) 
     except socket.error, socket.timeout: 
      logging.error('Something is wrong with host: %s' % host) 
      continue 

     # Send the data 
     s.sendall('"%s" "%s"' % (title, name)) 
     data = s.recv(size) 

     # Shutdown and close the socket 
     s.shutdown(socket.SHUT_RDWR) 
     s.close() 

    if len(sys.argv) > 1: 
     name = ' '.join(sys.argv[1:]) 
     network_notify(name) 
    sys.exit() 
+0

謝謝你做到了。所以它要麼沒有正確創建連接,要麼沒有正確關閉它。非常感謝,我不會想出來的。 – James 2012-08-04 16:26:04