473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

threading+ftp+import => block

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
Jul 24 '06 #1
2 2007
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
Jul 26 '06 #2
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
Jul 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
3
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
by: dbandler | last post by:
I'm trying to use ftp in python in a multi-threaded way on a windows box - python version 2.4.3. Problem is that it appears that it's only possible to have five instances/threads at one point in...
12
by: johnny | last post by:
I have taken a look at the code that dose one download at time, in multi threaded manner: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465531 What I wanted to do is, make it download...
5
by: johnny | last post by:
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...
3
by: johnny | last post by:
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...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 342 open (-38) / 3712 closed (+54) / 4054 total (+16) Bugs : 951 open (-14) / 6588 closed (+33) / 7539 total (+19) RFE : 257 open...
2
by: npcarp | last post by:
Is this hard? I am introducing Python to my students and want to make a simple, interactive program that will, while it's running, 'listen' for any keyboard events. That is, I don't want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.