Python Thread: Unterschied zwischen den Versionen

Aus xinux.net
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „<pre> #!/usr/bin/python3 import time import threading # set global variable flag flag = 1 colour="red" def normal(): global colour global flag wh…“)
 
 
Zeile 23: Zeile 23:
 
       global flag
 
       global flag
 
       touch=input('Press a key \n')
 
       touch=input('Press a key \n')
 +
      keystrk=touch.rstrip()
 +
      print ( "keystrk: " + keystrk)
 +
      if keystrk == "r":
 +
        print("You type r")
 +
        colour="red"
 +
      if keystrk == "g":
 +
        colour="green"
 +
      if keystrk == "y":
 +
        colour="yellow"
 +
      if keystrk == "q":
 +
        colour="yellow"
 +
        flag=False
 +
      #print("colour: " + colour)
 +
 +
n=threading.Thread(target=normal)
 +
i=threading.Thread(target=get_input)
 +
n.start()
 +
i.start()
 +
</pre>
 +
 +
<pre>
 +
#!/usr/bin/python3
 +
import time
 +
import threading
 +
 +
 +
# set global variable flag
 +
flag = 1
 +
colour="red"
 +
oldcolour="green"
 +
def normal():
 +
    global oldcolour
 +
    global colour
 +
    global flag
 +
    while flag==1:
 +
      if oldcolour != colour:
 +
        print(colour)
 +
        time.sleep(2)
 +
        if flag==False:
 +
            print('The while loop is now closing')
 +
        oldcolour=colour
 +
def get_input():
 +
    global colour
 +
    keystrk = "r"
 +
    while  keystrk != "q":
 +
      global flag
 +
      #touch=input('Press a key \n')
 +
      touch=input()
 
       keystrk=touch.rstrip()
 
       keystrk=touch.rstrip()
 
       print ( "keystrk: " + keystrk)
 
       print ( "keystrk: " + keystrk)

Aktuelle Version vom 29. November 2017, 19:41 Uhr

#!/usr/bin/python3
import time
import threading


# set global variable flag
flag = 1
colour="red"
def normal():
    global colour
    global flag
    while flag==1:
        print(colour)
        time.sleep(2)
        if flag==False:
            print('The while loop is now closing')

def get_input():
    global colour
    keystrk = "r"
    while   keystrk != "q":
      global flag
      touch=input('Press a key \n')
      keystrk=touch.rstrip()
      print ( "keystrk: " + keystrk)
      if keystrk == "r":
        print("You type r")
        colour="red"
      if keystrk == "g":
        colour="green"
      if keystrk == "y":
        colour="yellow"
      if keystrk == "q":
        colour="yellow"
        flag=False
      #print("colour: " + colour)

n=threading.Thread(target=normal)
i=threading.Thread(target=get_input)
n.start()
i.start()
#!/usr/bin/python3
import time
import threading


# set global variable flag
flag = 1
colour="red"
oldcolour="green"
def normal():
    global oldcolour
    global colour
    global flag
    while flag==1:
      if oldcolour != colour:
        print(colour)
        time.sleep(2)
        if flag==False:
            print('The while loop is now closing')
        oldcolour=colour
def get_input():
    global colour
    keystrk = "r"
    while   keystrk != "q":
      global flag
      #touch=input('Press a key \n')
      touch=input()
      keystrk=touch.rstrip()
      print ( "keystrk: " + keystrk)
      if keystrk == "r":
        print("You type r")
        colour="red"
      if keystrk == "g":
        colour="green"
      if keystrk == "y":
        colour="yellow"
      if keystrk == "q":
        colour="yellow"
        flag=False
      #print("colour: " + colour)

n=threading.Thread(target=normal)
i=threading.Thread(target=get_input)
n.start()
i.start()