omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Python not wait for sub Function

    Pythonista
    3
    19
    3332
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DavinE
      DavinE @cvp last edited by

      @cvp said:

      @DavinE and what is the code of self.setFileNamePDF ?

      this is my complete self.setFileNamePDF:

      @ui.in_background
          def setFileNamePDF(self, customer, customer_project, materiallistType):
              self.createLog(False, 'setFileNamePDF -- Aufruf der Funktion - Dateiname für die *.pdf Datei finden', '', 1)
              
              materiallistFilesName = None
              self.Synology_DS718(customer)
              try:
                  decontrol = None
                  for directory in self.connection_FTP.nlst(self.mainDirCustomer__CONFIG):
                      if directory.encode('latin-1').decode('utf-8') == self.subDirCustomer__CUSTOMER:
                          decontrol = True
                          break
                  if decontrol:
                      subDirCustomer_Materiallisten__CUSTOMER = '%s/%s' % (self.subDirCustomer_Materiallisten, customer_project)
                      subDirCustomer_Eintragungen = '%s/%s' % (subDirCustomer_Materiallisten__CUSTOMER, self.subDirCustomer_Eintragungen__CONFIG)
                      subDirCustomer_ZwischenRechnung = '%s/%s' % (subDirCustomer_Materiallisten__CUSTOMER, self.subDirCustomer_ZwischenRechnung__CONFIG)
                      subDirCustomer_Abgeschlossen = '%s/%s' % (subDirCustomer_Materiallisten__CUSTOMER, self.subDirCustomer_Abgeschlossen__CONFIG)
      
                      if materiallistType == self.subDirCustomer_Eintragungen__CONFIG:
                          materiallistType__Path = subDirCustomer_Eintragungen
                      elif materiallistType == self.subDirCustomer_ZwischenRechnung__CONFIG:
                          materiallistType__Path = subDirCustomer_ZwischenRechnung
                      elif materiallistType == self.subDirCustomer_Abgeschlossen__CONFIG:
                          materiallistType__Path = subDirCustomer_Abgeschlossen
                      else:
                          print(f'[ 7000 ] -- Der Materiallisten Typ konnte nicht ermittelt werden: [ {materiallistType} ]:\n{e}')
                          sound.play_effect(play_sound('play_effect', 'mysql', 'error'))
                          self.createLog(False, '[ 7000 ]', '', 2)
                          self.createLog(False, f'Der Materiallisten Typ konnte nicht ermittelt werden: [ {materiallistType} ]', '', 2)
                          console.alert('ftp-error', 'Der Materiallisten Typ konnte nicht ermittelt werden:\n[ {materiallistType} ]', hide_cancel_button=False)
      
                      if materiallistType == self.subDirCustomer_Abgeschlossen__CONFIG:
                          print("Abfrage drine!!")
                          materiallistFilesName =  f'{date.today()} -- {customer_project}.pdf'
                      else:
                          for i, files in enumerate(self.connection_FTP.nlst(materiallistType__Path)):
                              for n in range(1, 51):
                                  if os.path.basename(files).encode('latin-1').decode('utf-8') == f'{date.today()}__{n} -- {customer_project}.pdf':
                                      materiallistFilesName = f'{date.today()}__{(n + 1)} -- {customer_project}.pdf'
                              if (len(self.connection_FTP.nlst(materiallistType__Path)) - 1) is i:
                                  if not materiallistFilesName:
                                      materiallistFilesName =  f'{date.today()}__1 -- {customer_project}.pdf'
                                  break
                          else:
                              materiallistFilesName = f'{date.today()}__1 -- {customer_project}.pdf'
              except ftplib.all_errors as e:
                  print(f'[ 7001 ] -- Error bei der Funktion [ setFileNamePDF ]:\n{e}')
                  sound.play_effect(play_sound('play_effect', 'mysql', 'error'))
                  self.createLog(False, '[ 7001 ]', '', 2)
                  self.createLog(False, 'Error bei der Funktion [ setFileNamePDF ]', '', 2)
                  self.createLog(False, f'FTP-Server Fehler: {e}', '', 2)
                  console.alert('ftp-error', 'Kundenordner konnten nicht erstellt werden. [ Kunde Erstellen ]\nFehlerausgabe im Terminal', hide_cancel_button=False)
              finally:
                  self.connection_FTP.quit()
                  
              print(materiallistFilesName)
              return os.path.join(os.path.dirname(sys.argv[0]), 'Reportlab', materiallistFilesName)
      
      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @DavinE last edited by

        @DavinE is your @ui.in_background needed?

        1 Reply Last reply Reply Quote 0
        • DavinE
          DavinE last edited by

          @cvp, Honestly ? xD

          i have no idea why i set this....

          what does @ui.in_background and @on_main_thread exactly

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @DavinE last edited by

            @DavinE There are two base threads in Pythonista and you can force to run a process in The main_thread or in tHe ui one.
            Remove this line and it is still not ok, put the line @on_main_thread

            1 Reply Last reply Reply Quote 0
            • DavinE
              DavinE last edited by

              @cvp like this:

              @on_main_thread
              def generatePDF(...)
              

              and here:

              def setFileNamePDF(self, customer, customer_project, materiallistType)
              

              nothing ?

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @DavinE last edited by

                @DavinE Sincerely, I don't know but personally I would put the @on_main_thread before the other def.

                DavinE 1 Reply Last reply Reply Quote 0
                • DavinE
                  DavinE last edited by

                  @cvp okay thanks i try it and response to you xD

                  1 Reply Last reply Reply Quote 0
                  • DavinE
                    DavinE @cvp last edited by

                    @cvp said:

                    @DavinE Sincerely, I don't know but personally I would put the @on_main_thread before the other def.

                    it works xD Thanks @cvp

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @DavinE last edited by cvp

                      @DavinE eureka

                      1 Reply Last reply Reply Quote 0
                      • montykit
                        montykit last edited by

                        Hi everyone,

                        Is it possible to launch a python command without waiting for a return ? and therefore start the next command without waiting the previous one to finish ?

                        I searched and asked also on StackOverflow but so far I didn't found what I was looking for...

                        something that look promising might be the
                        1
                        concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
                        as seen here -> https://stackoverflow.com/a/60832288/11943028

                        But this example is focus on results that I don't need. any ideas ?

                        Thank you.


                        https://fashionmentions.com/best-foundation/

                        DavinE cvp 3 Replies Last reply Reply Quote 0
                        • DavinE
                          DavinE @montykit last edited by

                          @montykit,

                          in my case @ui.in_backgroundworks... and don't wait for a return

                          maybe for you too

                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @montykit last edited by cvp

                            @montykit see the threading module, you could start one thread for each command

                            But what do you call a command?

                            1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp @montykit last edited by cvp

                              @montykit try

                              import threading
                              
                              class MyThread(threading.Thread):
                              	def __init__(self,cmd):
                              		threading.Thread.__init__(self)
                              		self.run = cmd
                              
                              def cmd1():	
                              	s = 0
                              	for i in range(1000):
                              		s += 1
                              	print(s)
                              	
                              def cmd2():	
                              	s = 0
                              	for i in range(1000):
                              		s += i
                              	print(s)
                              
                              
                              MyThread(cmd1).start()
                              MyThread(cmd2).start()
                              
                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              Powered by NodeBB Forums | Contributors