python-thread mit start_new_thread nicht funktioniert

Bin in der Notwendigkeit, einen thread für meine python-app,

in meinem test habe ich einen Zähler mit einem timer für die Simulation der Schleife muss ich laufen, aber das problem ist, dass diese Schleife ruft Geldstrafe von einer grundlegenden python-Beispiel für den thread, aber nicht an meinem code, ich muss den Aufruf der Methode falsch?

hier mein code [das problem]

import thread  
import threading
from threading import Thread
import time

from Tkinter import *
from Tkinter import Tk

import sys

root = Tk()
mainframe = Frame(root) #  mainframe contained by root!, init

class myclass():

    def __init__(self):
        self.main() #atencion! es un loop! se quedara aqui!
        # en objC [self main]

    def submitForm(self,*args):
        print "submitalo"
        thread.start_new_thread( print_time, ("Thread-2", 4, ))


    def print_time( threadName, delay):
        count = 0
        while count < 5:
            time.sleep(delay)
            count += 1
            print "%s: %s" % ( threadName, time.ctime(time.time()) )

    def main(self): 
        print("from main")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) #  add subview mainframe
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)
        button = Button(mainframe, text='Submit', command=self.submitForm)
        button.grid(column=1 , row=3, sticky=(W,E))  

    #my loop
        root.mainloop()

if __name__ == "__main__":
    myclass()

Und hier die Probe funktionierenden code mit Gewinde

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1:
   pass

Dank

  • Das ist eine Menge code zu schauen! Können Sie entfernen Sie nicht benötigte Zeilen aus deinem ersten code-block, so dass wir alle konzentrieren sich auf das threading-problem?
  • danke, ich habe vereinfacht, um die max code .)
  • mit all Ihren try:...except:... - Blöcke, Sie scheinen konvertieren jede nützliche debugging-Meldungen in print "Error!". Welche Fehler haben Sie, wenn Sie entfernen alle Ausnahme "handling" - code?
  • hi @Karmastan Dank, nicht versuchen, außer jetzt, bekomme ich ... NameError: global name 'print_time' ist nicht definiert ... - Methode bearbeitet, die auf meinen code-block vielen Dank
Schreibe einen Kommentar