2016-03-05 37 views
2

我尝试使用我的脚本thread,但我得到这个错误:错误与Python线程

Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr

我的脚本:

# -*- coding: utf-8 -*- 

import tweepy 
import thread 

consumer_key = "" 
consumer_secret = "" 
access_key = "" 
access_secret = "" 


def deleteThread(api, objectId): 
    try: 
     api.destroy_status(objectId) 
     print "Deleted:", objectId 
    except: 
     print "Failed to delete:", objectId 

def oauth_login(consumer_key, consumer_secret): 
    """Authenticate with twitter using OAuth""" 

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth_url = auth.get_authorization_url() 

    verify_code = raw_input("Authenticate at %s and then enter you verification code here > " % auth_url) 
    auth.get_access_token(verify_code) 

    return tweepy.API(auth) 

def batch_delete(api): 
    print "You are about to Delete all tweets from the account @%s." % api.verify_credentials().screen_name 
    print "Does this sound ok? There is no undo! Type yes to carry out this action." 
    do_delete = raw_input("> ") 
    if do_delete.lower() == 'yes': 
     for status in tweepy.Cursor(api.user_timeline).items(): 
      try: 
       thread.start_new_thread(deleteThread, (api, status.id,)) 
      except: 
       print "Failed to delete:", status.id 

if __name__ == "__main__": 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_key, access_secret) 
    api = tweepy.API(auth) 
    print "Authenticated as: %s" % api.me().screen_name 

    batch_delete(api) 

如何解决这个问题呢?

+0

任何原因,你正在使用'thread',而不是'threading'? – cdarke

+0

@cdarke不,我只是尝试学习和使用python线程。 –

+0

我认为你的问题在于你不会等待线程完成,并且在线程仍在运行时解释器试图退出时引发错误。你的睡眠只是让你的线程有更多的时间退出。但它也减缓了一切。如果你移动到'threading'模块,它将等待它的线程退出(除非它们也被设置为守护进程)。 – tdelaney

回答

0

更新

I just solve my problem by adding time.sleep(1) before

thread.start_new_thread(deleteThread, (api, status.id,))