Python单独的线程正在杀死整个程序

时间:2013-10-06 20:25:49

标签: python multithreading crash

所以我写了这个剧本:

def schedule_setup():
# KILL OLD THREADS SHOULD THEY EXIST
global active
active = False
time.sleep(3)
active = True
global threadlist
threadlist = []

try:
    sql = "SELECT TIME_TO_RUN FROM time_table"
    cursor.execute(sql)
    results = cursor.fetchall()
    for row in results:
        #row[0].strftime('%H:%M')
        t = threading.Thread(target=th,args=(row[0].strftime('%H:%M'),))
        t.start()
        threadlist.append(t)
    # JOIN all threads to main memory
    #for count in threadlist:
        #count.join()
    sql = "UPDATE motor SET UPDATE_SCHEDULE = 0"
    try:
        cursor.execute(sql)
        # commit the changes in database
        db.commit()
    except:
        # Rollback in case there is any error
        print "no worky"
        db.rollback()
except:
    print "Error: UNABLE TO GET TABLE DATA"

需要在sql上设置时间并创建一个预定事件来执行操作。我把所有活动线程的线程“杀手”放在一开始,这样如果我再次调用这个线程,因为时间已经更新,它可以杀死旧的并用新的替换它们。这一切都是我想要的,但是一旦调用了动作,整个程序就会崩溃......这里是它所调用的代码:

def th(run_time):
    global active
    schedule.every().day.at(run_time).do(run_motor)

    while active == True:
        schedule.run_pending()
        time.sleep(1)

看看线程如何每秒检查一次?因此当我尝试创建新线程时线程被杀死,但是当“run_motor”被调用时,后面的主程序应该无限循环崩溃,有时其他线程仍在继续,所以这对我来说都很奇怪。

1 个答案:

答案 0 :(得分:0)

无法 sanely杀死一个帖子。而不是杀死一个线程,代码线程做什么,只做你希望它做什么。这样,你就没有必要杀掉它了。

如果你和另外两个人共用一辆车并且车子不在车道上,你不能只是杀死驾驶汽车的人。这将释放汽车供您使用,但它不会在车道上,因为您在有机会将其击退之前杀死了驾驶员。相反,打电话给他们并告诉他们把汽车带回来让他们在完成后放下汽车。

相关问题