473,403 Members | 2,293 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,403 software developers and data experts.

Threaded server

Hi,
I'm trying to run an asynchronous FTP server I wrote into a thread for
being able to run a test suite against it.
The code below is the threaded FTP server code I'm using:
--- snippet ---
class FTPd(threading.Thread):

def __init__(self):
self.active = False
threading.Thread.__init__(self)

def start(self, flag=None):
assert not self.active
self.flag = flag
threading.Thread.start(self)

def run(self):
assert not self.active
ftpd = ftpserver.FTPServer(address, ftp_handler)
if self.flag:
self.flag.set()
self.active = True
while self.active:
ftpd.server_forever(timeout=1, count=1)
ftpd.close()

def stop(self):
assert self.active
self.active = False

flag = threading.Event()
ftpd = FTPd()
ftpd.start(flag)
flag.wait() # wait for it to start
unittest.main() # run the test suite
ftpd.stop()
--- /snippet ---
Sometimes I get a strange error when all the tests have finished, the
server is stopped and Python is exiting:

----------------------------------------------------------------------
Ran 50 tests in 1.515s

OK
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPHandler.__del__ of <pyftpdlib.ftpserver.FTPHandler connected
127.0.0.1:2
249 at 0xa4b080>ignored
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPServer.__del__ of <pyftpdlib.ftpserver.FTPServer listening
127.0.0.1:543
21 at 0x9e1a30>ignored
I sincerely don't know why that happens but it's likely because I'm
not using threads properly.
My concern is that this could be caused by a sort of race condition
(e.g. Python tries to exit when ftpd.close call is not yet completed).

I tried to put a lock in the close() method for waiting the run()
method to be completed before returning but I didn't solve the
problem.
Another information, in case it could be useful, is that this seems to
happen with Python 2.3 only.
By using 2.4 and Python 2.5 I have no problems.

In such cases which is the right way for doing things?
Using setDaemon(True)?
Could someone point me in the right direction?
I've always used the asynchronous approach and dealing with threads is
a real pain for me.
Thanks in advance.

-- Giampaolo
Jan 14 '08 #1
3 2710
Giampaolo Rodola' <gn****@gmail.comwrote:
I'm trying to run an asynchronous FTP server I wrote into a thread for
being able to run a test suite against it.
The code below is the threaded FTP server code I'm using:

class FTPd(threading.Thread):

def __init__(self):
self.active = False
threading.Thread.__init__(self)

def start(self, flag=None):
assert not self.active
self.flag = flag
threading.Thread.start(self)

def run(self):
assert not self.active
ftpd = ftpserver.FTPServer(address, ftp_handler)
if self.flag:
self.flag.set()
self.active = True
while self.active:
ftpd.server_forever(timeout=1, count=1)
ftpd.close()

def stop(self):
assert self.active
self.active = False

flag = threading.Event()
ftpd = FTPd()
ftpd.start(flag)
flag.wait() # wait for it to start
unittest.main() # run the test suite
ftpd.stop()

Sometimes I get a strange error when all the tests have finished, the
server is stopped and Python is exiting:

Ran 50 tests in 1.515s

OK
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPHandler.__del__ of <pyftpdlib.ftpserver.FTPHandler connected
127.0.0.1:2
249 at 0xa4b080>ignored
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPServer.__del__ of <pyftpdlib.ftpserver.FTPServer listening
127.0.0.1:543
21 at 0x9e1a30>ignored
I sincerely don't know why that happens but it's likely because I'm
not using threads properly.
My concern is that this could be caused by a sort of race condition
(e.g. Python tries to exit when ftpd.close call is not yet
completed).
It looks like when python is shutting down, it has removed an object
the ftphandler code relies on.

I see you attempt to kill the ftp server with ftpd.stop(). That is
good, but you don't wait for the thread to finish (it might take up to
a second in ftpd.server_forever if I understand correctly).

I expect if you put a self.join() at the end of the stop() method the
problem will go away.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jan 14 '08 #2
On 14 Gen, 12:30, Nick Craig-Wood <n...@craig-wood.comwrote:
Giampaolo Rodola' <gne...@gmail.comwrote:
*I'm trying to run an asynchronous FTP server I wrote into a thread for
*being able to run a test suite against it.
*The code below is the threaded FTP server code I'm using:
*class FTPd(threading.Thread):
* * *def __init__(self):
* * * * *self.active = False
* * * * *threading.Thread.__init__(self)
* * *def start(self, flag=None):
* * * * *assert not self.active
* * * * *self.flag = flag
* * * * *threading.Thread.start(self)
* * *def run(self):
* * * * *assert not self.active
* * * * *ftpd = ftpserver.FTPServer(address, ftp_handler)
* * * * *if self.flag:
* * * * * * *self.flag.set()
* * * * *self.active = True
* * * * *while self.active:
* * * * * * *ftpd.server_forever(timeout=1, count=1)
* * * * *ftpd.close()
* * *def stop(self):
* * * * *assert self.active
* * * * *self.active = False
*flag = threading.Event()
*ftpd = FTPd()
*ftpd.start(flag)
*flag.wait() *# wait for it to start
*unittest.main() # run the test suite
*ftpd.stop()
*Sometimes I get a strange error when all the tests have finished, the
*server is stopped and Python is exiting:
*Ran 50 tests in 1.515s
*OK
*Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
*thod FTPHandler.__del__ of <pyftpdlib.ftpserver.FTPHandler connected
*127.0.0.1:2
*249 at 0xa4b080>ignored
*Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
*thod FTPServer.__del__ of <pyftpdlib.ftpserver.FTPServer listening
*127.0.0.1:543
*21 at 0x9e1a30>ignored
*I sincerely don't know why that happens but it's likely because I'm
*not using threads properly.
*My concern is that this could be caused by a sort of race condition
*(e.g. Python tries to exit when ftpd.close call is not yet
*completed).

It looks like when python is shutting down, it has removed an object
the ftphandler code relies on.

I see you attempt to kill the ftp server with ftpd.stop(). *That is
good, but you don't wait for the thread to finish (it might take up to
a second in ftpd.server_forever if I understand correctly).

I expect if you put a self.join() at the end of the stop() method the
problem will go away.

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
Tried it but the problem remains.
The strange thing is that it sometimes happens, sometimes doesn't.
Jan 14 '08 #3
Giampaolo Rodola' wrote:
On 14 Gen, 12:30, Nick Craig-Wood <n...@craig-wood.comwrote:
>Giampaolo Rodola' <gne...@gmail.comwrote:
I'm trying to run an asynchronous FTP server I wrote into a thread for
being able to run a test suite against it.
The code below is the threaded FTP server code I'm using:
class FTPd(threading.Thread):
def __init__(self):
self.active = False
threading.Thread.__init__(self)
def start(self, flag=None):
assert not self.active
self.flag = flag
threading.Thread.start(self)
def run(self):
assert not self.active
ftpd = ftpserver.FTPServer(address, ftp_handler)
if self.flag:
self.flag.set()
self.active = True
while self.active:
ftpd.server_forever(timeout=1, count=1)
ftpd.close()
def stop(self):
assert self.active
self.active = False
flag = threading.Event()
ftpd = FTPd()
ftpd.start(flag)
flag.wait() Â*# wait for it to start
unittest.main() # run the test suite
ftpd.stop()
Sometimes I get a strange error when all the tests have finished, the
server is stopped and Python is exiting:
Ran 50 tests in 1.515s
OK
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPHandler.__del__ of <pyftpdlib.ftpserver.FTPHandler connected
127.0.0.1:2
249 at 0xa4b080>ignored
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<bound me
thod FTPServer.__del__ of <pyftpdlib.ftpserver.FTPServer listening
127.0.0.1:543
21 at 0x9e1a30>ignored
I sincerely don't know why that happens but it's likely because I'm
not using threads properly.
My concern is that this could be caused by a sort of race condition
(e.g. Python tries to exit when ftpd.close call is not yet
completed).

It looks like when python is shutting down, it has removed an object
the ftphandler code relies on.

I see you attempt to kill the ftp server with ftpd.stop(). Â*That is
good, but you don't wait for the thread to finish (it might take up to
a second in ftpd.server_forever if I understand correctly).

I expect if you put a self.join() at the end of the stop() method the
problem will go away.

--
Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick-
Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Tried it but the problem remains.
The strange thing is that it sometimes happens, sometimes doesn't.
AFAIK you can safely ignore this error. It essentially stems from
non-determinism when shutting down threads.

Diez
Jan 14 '08 #4

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

Similar topics

0
by: David | last post by:
I've installed the DBD::Proxy and RPC::PLServer modules on Windows 2000 accessing SQL Server 2000 (through an ODBC system DSN). I can interface to the database from one or more Linux boxes just...
0
by: Royco | last post by:
Hi, Situation as follows - a C++ component with OCI-calls running under the control of COM (w2k) - component is marked transactional - the component connects to database1 but wants to update...
5
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a...
14
by: Snor | last post by:
I'm attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven...
5
by: David Thielen | last post by:
I assumed that requests to my app are multi-threaded in that is there are 2 browsers making requests at the same time, I could be in the middle of responding to one when I get the next. But what...
0
by: chsalvia | last post by:
I'm attempting to write a simple multi-threaded webserver on UNIX that uses a thread pool to handle connections. I use an accept() loop which simply sends requests to a request function, and then...
2
by: Jay Loden | last post by:
All, In studying Python, I have predictably run across quite a bit of talk about the GIL and threading in Python. As my day job, I work with a (mostly Java) application that is heavily threaded....
1
by: J | last post by:
Hi, I've written a multi threaded application which scans about 2000 servers event logs to check for disk errors. The problem with it is due to the fact that it just keeps eating memory. In...
1
by: nataraj2502 | last post by:
Hi, I have just configured my C# multi-threaded application from 32 bit to 64 bit platform for getting more memory support. But my application runs up to 40 - 50% slower as compared to 32 bit...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.