2016-06-02 16 views
1

ich Python bin neu, ich versuche, ein kleines Programm zu schreiben, die Dateien finden aktualisiert und dann im Verzeichnis aktualisieren (das funktioniert)mehr Funktionen gleichzeitig ausführen

Das Problem, das ich bin Das Gegenüber besteht darin, dass ich nicht zwei Funktionen gleichzeitig ausführen kann. Beispiel: Während der Code nach der Datei sucht und sie ersetzt, wird auch ein kleines Popupfenster angezeigt, in dem eine Textnachricht angezeigt wird, die das System beschäftigt. und nachdem der Code abgeschlossen ist, wird eine Popup-Nachricht "Completed" angezeigt.

!/usr/bin/python 
# -*- coding: utf-8 -*- 
#Shell script version 2 
import os 
import shutil 
from Tkinter import * 
import tkMessageBox 
import tkSimpleDialog 
from threading import Thread 
import threading 
import time 
from multiprocessing import Process 
import sys 
from tkinter.ttk import * 
import multiprocessing 


Search = 'c:\\' 
search_folder = '' 


class popupWindow(object): 
    def __init__(self,parent): 
     top=self.top=Toplevel(parent) 
     self.l=Label(top,text="Patch is running") 
     self.l.pack() 
     self.b=Button(top,text='Ok',command=self.cleanup) 
     self.b.pack() 
    def cleanup(self): 
     #self.value=self.e.get() 
     self.top.destroy() 

class Example(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 

     self.parent = parent 

     self.initUI() 

    def initUI(self): 
     photo = PhotoImage(file="C:\\IMAGE.gif") 
     self.parent.title("Off Line Playe Patch") 
     self.style = Style() 
     self.style.theme_use("default") 

     self.pack(fill=BOTH, expand=1) 

     W = Label(self,image=photo) 
     W.photo = photo 
     W.pack() 
     #Button(tk,text='foo',command=bar).pack() 
     quitButton = Button(self, text="Apply Patch",command=self.StPatch) 
     quitButton.place(x=80, y=0) 


    def StPatch(self): 
     # Replaces shell.htm with correct file 

     def FoundShell(): 

      for root, dirs, files in os.walk(search_folder): 
      for file in files: 
      print os.path.join(root,file) 
      if file == 'shell.htm': 
      print "found file" 
      Shell = os.path.join(root,file) 
      os.remove(Shell) 
      shutil.copy2('./shell.htm',Shell) 


    def Hello(): 
     self.w = popupWindow(self.parent) 
     self.parent.wait_window(self.w.top) 
     i = 0 
     while i < 100000: 
      print('hello world') 
      i=i+1 


     if os.path.exists('C:\Player - Office 2010'): 
      print 'Found Folder' 
      search_folder = 'C:\Player - Office 2010' 
      tkMessageBox.showinfo("Update Done", "Update has been applyed successfuly ", command=FoundShell()) 
     else: # request user to enter path to Directory 
      print 'not found' 
      search_folder = tkSimpleDialog.askstring('Path', 'Please enter Path to Offline Player',) 
      print search_folder 
      tkMessageBox.showinfo("Update Done", "Update has been applyed successfuly ", command=FoundShell()) 

     #t1 = threading.Thread(target=RunScript(self)) 
     #t1.start() 
     #t1.join() 
     #Hello() 




    # the join means wait untill it finished 

root = Tk() 
app = Example(root) 

root.mainloop() 
+0

Haben Sie Multi Threading versucht? – qvpham

Antwort

0
import thread 
from template import * 
from graph import * 

try: 
    thread.start_new_thread(main,()) 
    thread.start_new_thread(graph,()) 
except KeyboardInterrupt: 
    thread.exit() 

while 1: 
    pass 

Dies ist ein Beispiel dafür, wie mit Multithreads zu einem Zeitpunkt 2 Funktionen aufzurufen. Aufruf der Funktion main ohne Parameter "()" aus dem Template-Dateimodul und Funktion ohne Parameter "()" aus der Modulgrafik.

und verwenden Sie diese in eine Hilfsdatei namens run.py

Der while 1 den Faden am Laufen zu halten. bis KeyboardInterrupt wird behandelt.

Ich wünschte, dies könnte helfen!

+0

Beachten Sie auch die _Global Interpreter Lock_ (GIL): https://wiki.python.org/moin/GlobalInterpreterLock – moooeeeep

Verwandte Themen