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

newb: How to call one modue from other

P: n/a
I have a module called ftp and I have another module called
processKick. What I need is to have processKick, create fork and
execute ftp like below.

Relevant processKick code as follows:

def do_child_stuff():
ftp

def fork_test():
pid = os.fork()
if pid == 0:
# child
do_child_stuff()
os._exit(0)
# parent - wait for child to finish
os.waitpid(pid, os.P_WAIT)

Can someone also tell me what is the purpose of
if __name__ == "__main__":

Do I have to call, main of ftp module within processKick?

Thank you in advance

Dec 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
johnny wrote:
I have a module called ftp and I have another module called
processKick. What I need is to have processKick, create fork and
execute ftp like below.

Relevant processKick code as follows:

def do_child_stuff():
ftp

def fork_test():
pid = os.fork()
if pid == 0:
# child
do_child_stuff()
os._exit(0)
# parent - wait for child to finish
os.waitpid(pid, os.P_WAIT)
Here is my ftp module:

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)
fp = open('H:/ftp_download/' + p, 'wb')
try: c.retrbinary('RETR %s' % e, fp.write)
finally: fp.close()
finally: c.close()

tq.task_done()

if __name__ == '__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('.doc'):
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.'

Can someone also tell me what is the purpose of
if __name__ == "__main__":

Do I have to call, main of ftp module within processKick?

Thank you in advance

Dec 7 '06 #2

P: n/a
At Wednesday 6/12/2006 23:20, johnny wrote:
>Can someone also tell me what is the purpose of
if __name__ == "__main__":
Reading the Python Tutorial helps a lot.
>Do I have to call, main of ftp module within processKick?
Yes, because the original script author didn't want to call a
function inside that module, instead, he wanted to invoke the module
as a child program.
Reading the Python Tutorial helps a lot.
--
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 7 '06 #3

P: n/a

Gabriel Genellina wrote:
Reading the Python Tutorial helps a lot.
I did read "Dive Into Python", one week ago. It's a good book, but it
didn't cover this kind of situation.

Dec 7 '06 #4

P: n/a
"johnny" <ra*******@gmail.comwrites:
Gabriel Genellina wrote:
Reading the Python Tutorial helps a lot.
I did read "Dive Into Python", one week ago. It's a good book, but
it didn't cover this kind of situation.
Yes, it is good. It's also not the Python tutorial.

The Python tutorial is this document:

<URL:http://docs.python.org/tut/>

It's very beneficial to *work through* the tutorial, executing and
understanding each example before moving onto the next. (This is more
than "read the tutorial" that others sometimes suggest.)

--
\ "A man must consider what a rich realm he abdicates when he |
`\ becomes a conformist." -- Ralph Waldo Emerson |
_o__) |
Ben Finney

Dec 7 '06 #5

P: n/a
At Thursday 7/12/2006 01:03, johnny wrote:
>Gabriel Genellina wrote:
Reading the Python Tutorial helps a lot.
I did read "Dive Into Python", one week ago. It's a good book, but it
didn't cover this kind of situation.
Chapter 2. "Your First Python Program"
The *very*first* example does this.

Now that you know more about Python, read it again, or the Tutorial,
and actually *do* the exercises...
--
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 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.