Multithreaded python script calls the COMMAND LINE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnny

    #1

    Multithreaded python script calls the COMMAND LINE

    I have python script does ftp download in a multi threaded way. Each
    thread downloads a file, close the file, calls the comman line to
    convert the .doc to pdf. Command line should go ahead and convert the
    file. My question is, when each thread calls the command line, does one
    command line process all the request, or each thread creates a one
    command line process for themselves and executes the command? For some
    reason I am getting "File '1' is alread exists, Do you want to
    overwrite [y/n]?" I have to manually enter 'y' or 'n' for the python
    script to complete.

  • gagsl-py@yahoo.com.ar

    #2
    Re: Multithreaded python script calls the COMMAND LINE

    On 7 dic, 17:36, "johnny" <rampet...@gmai l.comwrote:
    I have python script does ftp download in a multi threaded way. Each
    thread downloads a file, close the file, calls the comman line to
    convert the .doc to pdf. Command line should go ahead and convert the
    file. My question is, when each thread calls the command line, does one
    command line process all the request, or each thread creates a one
    command line process for themselves and executes the command? For some
    reason I am getting "File '1' is alread exists, Do you want to
    overwrite [y/n]?" I have to manually enter 'y' or 'n' for the python
    script to complete.
    That depends on how you invoke it: os.system creates a new shell which
    in turn creates a new process; the spawn* functions do that directly.
    Anyway, if you have many conversion processes running, you should pass
    them unique file names to avoid conflicts.
    Where does the '1' name come from? If it's you, don't use a fixed name
    - the tempfile module may be useful.

    Comment

    • johnny

      #3
      Re: Multithreaded python script calls the COMMAND LINE


      gagsl...@yahoo. com.ar wrote:
      >
      That depends on how you invoke it: os.system creates a new shell which
      in turn creates a new process; the spawn* functions do that directly.
      I am using os.system. Here is my code

      import ftplib, posixpath, threading
      from TaskQueue import TaskQueue

      def worker(tq):
      while True:
      host, e = tq.get()

      c = ftplib.FTP(host )
      c.connect()
      try:
      c.login()
      p = posixpath.basen ame(e)
      ps_dir = r'H:/ftp_download/'
      filename = download_dir+p
      fp = open(filename, 'wb')
      try: c.retrbinary('R ETR %s' % e, fp.write)
      finally: fp.close()
      finally: c.close()
      if (p.lower().ends with('.ps') ):
      partFileName = p.split('.', 1)
      movedFile = download_dir + p
      #movedFile = p
      finalFile = ps_dir + partFileName[0]+'.pdf'
      encode_cmd = r'ps2pdf '+ movedFile + ' '+ finalFile
      os.system(encod e_cmd)

      tq.task_done()

      if __name__ == '__main__':
      main()

      def main():
      q = TaskQueue()
      #host = 'ftp.microsoft. com'
      host = 'mysite.com'
      c = ftplib.FTP(host )
      c.connect()
      try:
      #c.login()
      c.login()

      #folder = '/deskapps/kids/'
      folder = ''
      for n in c.nlst(folder):
      if n.lower().endsw ith('.ps'):
      q.put((host, n))


      finally: c.close()

      numworkers = 4
      for i in range(numworker s):
      t = threading.Threa d(target=worker , args=(q,))
      t.setDaemon(Tru e)
      t.start()

      q.join()
      print 'Done.'
      Anyway, if you have many conversion processes running, you should pass
      them unique file names to avoid conflicts.
      Where does the '1' name come from? If it's you, don't use a fixed name
      - the tempfile module may be useful.
      I am giving unique name to the converted file with .pdf. I think
      something up with the thread. I am new to python, I am not sure what's
      wrong.

      Comment

      • Gabriel Genellina

        #4
        Re: Multithreaded python script calls the COMMAND LINE

        At Thursday 7/12/2006 19:13, johnny wrote:
        Anyway, if you have many conversion processes running, you should pass
        them unique file names to avoid conflicts.
        Where does the '1' name come from? If it's you, don't use a fixed name
        - the tempfile module may be useful.
        >
        >I am giving unique name to the converted file with .pdf. I think
        >something up with the thread. I am new to python, I am not sure what's
        >wrong.
        Unless the ps2pdf program uses explicitely '1' as a filename, I don't
        see where it may come from. Your python code doesn't generate it -
        unless you have a 1.ps file.

        [Quoting from a previous message]
        >For some
        >reason I am getting "File '1' is alread exists, Do you want to
        >overwrite [y/n]?" I have to manually enter 'y' or 'n' for the python
        >script to complete.
        Is that '1' just an example, or you always get that?


        --
        Gabriel Genellina
        Softlab SRL

        _______________ _______________ _______________ _____
        Correo Yahoo!
        Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
        ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

        Comment

        Working...