newb: How to call one modue from other

J

johnny

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
 
J

johnny

johnny said:
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
 
G

Gabriel Genellina

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
 
J

johnny

Gabriel said:
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.
 
B

Ben Finney

johnny said:
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.)
 
G

Gabriel Genellina

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top