Python - spustenie vlakna viackrat

Sekcia: Programovanie 28.02 | 12:38
Avatar Bibibo   Návštevník

Ahojte,

Mam takyto jednoduchy skript s ktorym ovladam vystupy. Mam tam funkciu output_on v ktorej spustam styri vlakna na postupne zapinanie styroch vystupov. Ak to spravim raz, tak to funguje super, ale ak to chcem spravit este raz, tak mi napise, ze vlakna mozu byt spustene len raz. Som myslel, ze ked to vlakna vykonaju, tak sa ukoncia a mozem ich spustit znova.

from ablib import Pin
import time
#import sys
import sqlite3
import threading

#data = sys.argv[1]
temp_list = list()
conn = sqlite3.connect('database/database.db')
cur = conn.cursor()
cur.execute("SELECT restart_timer FROM output")
row = cur.fetchall()


def output_on(data):
  if data == "out1_on":
    output1=Pin('N4','OUTPUT')
    output1.on()	
   
		  
  if data == "out2_on":
    output2=Pin('N5','OUTPUT')
    output2.on()	
   
		
  if data == "out3_on":
    output3=Pin('N6','OUTPUT')
    output3.on()	
    
		  
  if data == "out4_on":
    output4=Pin('N7','OUTPUT')
    output4.on()
    
  if data == "glb_on":
     output1_thread.start()
     output2_thread.start()
     output3_thread.start()
     output4_thread.start()
        
    
def output1():
   output1=Pin('N4','OUTPUT')
   time.sleep(list(row[0])[0])
   output1.on()
  
   
def output2():
   output2=Pin('N5','OUTPUT')
   time.sleep(list(row[1])[0])
   output2.on()
   
   
def output3():
   output3=Pin('N6','OUTPUT')
   time.sleep(list(row[2])[0])
   output3.on()
   
   
def output4():
   output4=Pin('N7','OUTPUT')
   time.sleep(list(row[2])[0])
   output4.on()
   
    
def output_off(data):
  if data == "out1_off":
    output1=Pin('N4','OUTPUT')
    output1.off()	
   
		  
  if data == "out2_off":
    output2=Pin('N5','OUTPUT')
    output2.off()	
   
		
  if data == "out3_off":
    output3=Pin('N6','OUTPUT')
    output3.off()	
    
		  
  if data == "out4_off":
    output4=Pin('N7','OUTPUT')
    output4.off()
    
  if data == "glb_off":
    output1=Pin('N4','OUTPUT')
    output2=Pin('N5','OUTPUT')
    output3=Pin('N6','OUTPUT')
    output4=Pin('N7','OUTPUT')
    output1.off()
    output2.off()
    output3.off()
    output4.off()
    
output1_thread = threading.Thread(target = output1)
output2_thread = threading.Thread(target = output2)
output3_thread = threading.Thread(target = output3)
output4_thread = threading.Thread(target = output4)

    
    • RE: Python - spustenie vlakna viackrat 28.02 | 12:55
      Avatar Miroslav Bendík Gentoo  Administrátor

      Thread sa dá spustiť len raz. V tomto konkrétnom prípade by som radšej použil multiprocessing.Pool.map.

    • RE: Python - spustenie vlakna viackrat 28.02 | 14:27
      Avatar Zer0 Blbuntu 16.04 gnome-session-flashback | Debian 8  Používateľ

      output1_thread = threading.Thread(target = output1).start() output2_thread = threading.Thread(target = output2).start() output3_thread = threading.Thread(target = output3).start() output4_thread = threading.Thread(target = output4).start()

      pripadne pouzi len toto ↓↓↓↓ bez nazvu

      threading.Thread(target = output1).start() threading.Thread(target = output2).start() threading.Thread(target = output3).start() threading.Thread(target = output4).start()

      neviem, ci potrebujes mat nazov threadu...pises ze nakolko by mali byt vypnute...ony sa ukoncia same...ano...ale thread moze byt spusteny len raz...no, nic ti nebrani vytvorit novy, co robi to iste a spustit ho

      • RE: Python - spustenie vlakna viackrat 28.02 | 14:29
        Avatar Zer0 Blbuntu 16.04 gnome-session-flashback | Debian 8  Používateľ

        sorry za mišmaš

        output1_thread = threading.Thread(target = output1).start()

        output2_thread = threading.Thread(target = output2).start()

        output3_thread = threading.Thread(target = output3).start()

        output4_thread = threading.Thread(target = output4).start()

        pripadne pouzi len toto ↓↓↓↓ bez nazvu

        threading.Thread(target = output1).start()

        threading.Thread(target = output2).start()

        threading.Thread(target = output3).start()

        threading.Thread(target = output4).start()

        neviem, ci potrebujes mat nazov threadu...pises ze nakolko by mali byt vypnute...ony sa ukoncia same...ano...ale thread moze byt spusteny len raz...no, nic ti nebrani vytvorit novy, co robi to iste a spustit ho

        • RE: Python - spustenie vlakna viackrat 28.02 | 14:34
          Avatar Zer0 Blbuntu 16.04 gnome-session-flashback | Debian 8  Používateľ

          no a hod to sem↓↓↓samozrejme, ak to dobre chapem if data == "glb_on":

    • RE: Python - spustenie vlakna viackrat 28.02 | 17:32
      Avatar Bibibo   Návštevník
      from ablib import Pin
      import time
      import sqlite3
      import threading
      
      
      
      temp_list = list()
      conn = sqlite3.connect('database/database.db')
      cur = conn.cursor()
      cur.execute("SELECT restart_timer FROM output")
      row = cur.fetchall()
      
      
      def output_on(data):
        if data == "out1_on":
          output1=Pin('N4','OUTPUT')
          output1.on()    
         
                
        if data == "out2_on":
          output2=Pin('N5','OUTPUT')
          output2.on()    
         
              
        if data == "out3_on":
          output3=Pin('N6','OUTPUT')
          output3.on()    
          
                
        if data == "out4_on":
          output4=Pin('N7','OUTPUT')
          output4.on()
          
        if data == "glb_on":
            for i in range(1, 5):
          output_thread = global_on(i)
          output_thread.start()
          
      class global_on(threading.Thread):
        
        def __init__(self,i):
          threading.Thread.__init__(self)
          self.i = i
        
        def run(self):    
          if self.i == 1:
            output1=Pin('N4','OUTPUT')
            time.sleep(list(row[0])[0])
            output1.on()
          if self.i == 2:
            output2=Pin('N5','OUTPUT')
            time.sleep(list(row[1])[0])
            output2.on()
          if self.i == 3:
            output3=Pin('N6','OUTPUT')
            time.sleep(list(row[2])[0])
            output3.on()
          if self.i == 4:
            output4=Pin('N7','OUTPUT')
            time.sleep(list(row[2])[0])
            output4.on()
      
         
      def output_off(data):
        if data == "out1_off":
          output1=Pin('N4','OUTPUT')
          output1.off()    
         
                
        if data == "out2_off":
          output2=Pin('N5','OUTPUT')
          output2.off()    
         
              
        if data == "out3_off":
          output3=Pin('N6','OUTPUT')
          output3.off()    
          
                
        if data == "out4_off":
          output4=Pin('N7','OUTPUT')
          output4.off()
          
        if data == "glb_off":
          output1=Pin('N4','OUTPUT')
          output2=Pin('N5','OUTPUT')
          output3=Pin('N6','OUTPUT')
          output4=Pin('N7','OUTPUT')
          output1.off()
          output2.off()
          output3.off()
          output4.off()
      
          
      

      tak nakoniec som to spravil takto. Funguje o celkom dobre.