newb: How to call one modue from other

johnny rampeters at gmail.com
Wed Dec 6 21:20:32 EST 2006


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




More information about the Python-list mailing list