threading+ftp+import => block

Discussion in 'Python' started by Panard, Jul 24, 2006.

  1. Panard

    Panard Guest

    Hi,
    I'm experiencing a strange problem while trying to manage a ftp connection
    into a separate thread.

    I'm on linux, python 2.4.3

    Here is a test :
    ------ ftp_thread.py ------
    import ftplib
    import threading
    import datetime

    class test( threading.Thread ) :
    ftp_conn = ftplib.FTP("localhost","user","pass")
    def run( self ) :
    print self.ftp_conn.pwd()
    self.ftp_conn.dir("/")
    print datetime.date.today()
    def t() :
    t = test()
    t.start()
    t.join()
    t()
    -------

    If I do :
    $ python ftp_thread.py
    /
    drwxrwsr-x 2 panard ftp 4096 Jul 24 12:48 archives
    2006-07-24
    ==> Works perfectly

    But :
    $ python
    >>> import ftp_thread

    /
    <program block>

    For a strange reason, I've tried to add theses lines before the 't()' call :

    ftp_conn = ftplib.FTP("localhost","user","pass")
    ftp_conn.dir("/")

    And then, when calling t(), self.ftp_conn.dir("/") shows the list ! (I guess
    a problem of globals()/import .. ?)
    But... the program now blocks just before displaying today date...

    I guess I'm doing something really wrong with threads.. but I don't know
    what...

    Any hints?

    Thanks

    Panard
     
    Panard, Jul 24, 2006
    #1
    1. Advertising

  2. Panard

    Damjan Guest

    Panard wrote:

    > Hi,
    > I'm experiencing a strange problem while trying to manage a ftp connection
    > into a separate thread.
    >
    > I'm on linux, python 2.4.3
    >
    > Here is a test :
    > ------ ftp_thread.py ------
    > import ftplib
    > import threading
    > import datetime
    >
    > class test( threading.Thread ) :
    > ftp_conn = ftplib.FTP("localhost","user","pass")
    > def run( self ) :
    > print self.ftp_conn.pwd()
    > self.ftp_conn.dir("/")
    > print datetime.date.today()
    > def t() :
    > t = test()
    > t.start()
    > t.join()
    > t()
    > -------
    >
    > If I do :
    > $ python ftp_thread.py
    > /
    > drwxrwsr-x 2 panard ftp 4096 Jul 24 12:48 archives
    > 2006-07-24
    > ==> Works perfectly
    >
    > But :
    > $ python
    >>>> import ftp_thread

    > /
    > <program block>


    This has been documented in the blog posts titled "How well you know Python"
    (find it on google)

    The problem is that, once you run "import ftp_thread" a lock is set in the
    Python interpreter, so then you can't start a new thread... (this from the
    back of my mind).



    --
    damjan
     
    Damjan, Jul 26, 2006
    #2
    1. Advertising

  3. Panard

    Damjan Guest

    Damjan wrote:

    > Panard wrote:
    >
    >> Hi,
    >> I'm experiencing a strange problem while trying to manage a ftp
    >> connection into a separate thread.
    >>
    >> I'm on linux, python 2.4.3
    >>
    >> Here is a test :
    >> ------ ftp_thread.py ------
    >> import ftplib
    >> import threading
    >> import datetime
    >>
    >> class test( threading.Thread ) :
    >> ftp_conn = ftplib.FTP("localhost","user","pass")
    >> def run( self ) :
    >> print self.ftp_conn.pwd()
    >> self.ftp_conn.dir("/")
    >> print datetime.date.today()
    >> def t() :
    >> t = test()
    >> t.start()
    >> t.join()
    >> t()
    >> -------
    >>
    >> If I do :
    >> $ python ftp_thread.py
    >> /
    >> drwxrwsr-x 2 panard ftp 4096 Jul 24 12:48 archives
    >> 2006-07-24
    >> ==> Works perfectly
    >>
    >> But :
    >> $ python
    >>>>> import ftp_thread

    >> /
    >> <program block>

    >
    > This has been documented in the blog posts titled "How well you know
    > Python" (find it on google)
    >
    > The problem is that, once you run "import ftp_thread" a lock is set in the
    > Python interpreter, so then you can't start a new thread... (this from the
    > back of my mind).


    Actually, testing with
    class test( threading.Thread ) :
    def run( self ) :
    print 'Hello'

    instead of your test class, didn't show this anomally... actually the
    problem is that "import ftp_thread" sets a lock on *importing* other
    modules and I thing that the call to "self.ftp_conn.dir("/")" deep down in
    the ftplib module tries to "import re" and that's where it hangs.

    You could try to move the "import re" in ftplib to the top of the module,
    and submit a patch to the Python bug system.



    Here's a simpler test class that hangs:

    import threading
    class test(threading.Thread):
    import string
    print 'Step one'
    def run(self):
    import re
    print 'Hangs before this when this file is imported'

    def t() :
    t = test()
    t.start()
    t.join()
    t()



    --
    damjan
     
    Damjan, Jul 26, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morrell
    Replies:
    1
    Views:
    965
    roy axenov
    Oct 10, 2006
  2. Replies:
    9
    Views:
    1,048
    Mark Space
    Dec 29, 2007
  3. Steven Woody
    Replies:
    0
    Views:
    423
    Steven Woody
    Jan 9, 2009
  4. Steven Woody
    Replies:
    0
    Views:
    457
    Steven Woody
    Jan 9, 2009
  5. D. Buck
    Replies:
    2
    Views:
    493
    D. Buck
    Jun 29, 2004
Loading...

Share This Page