By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

Multithreaded python script calls the COMMAND LINE

P: n/a
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.

Dec 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 7 dic, 17:36, "johnny" <rampet...@gmail.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.

Dec 7 '06 #2

P: n/a

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.basename(e)
ps_dir = r'H:/ftp_download/'
filename = download_dir+p
fp = open(filename, 'wb')
try: c.retrbinary('RETR %s' % e, fp.write)
finally: fp.close()
finally: c.close()
if (p.lower().endswith('.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(encode_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().endswith('.ps'):
q.put((host, n))
finally: c.close()

numworkers = 4
for i in range(numworkers):
t = threading.Thread(target=worker, args=(q,))
t.setDaemon(True)
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.

Dec 7 '06 #3

P: n/a
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
Dec 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.