![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Banned
![]() ![]() |
CherryPy Multiple Threads
My problem now is that CherryPy can only handle one request at a time.
This is a VERY BIG problem. If someone is downloading a 100kb picture on dial-up, it can take so long that CherryPy times out. I've tried making a program that detects if CherryPy has timed out, but it can't because it will never even return a signal saying it has timed out when you access the page. I've been needing to reboot my server up to three times a day just because of the large amount of people accessing my site at the same time. I've searched the documentation for possibly a time-out if a response takes too long, couldn't find anything. But even if I could, people's downloads would cancel. The best answer would be a way to multi-thread using CherryPy. And I'm not ready to convert a 200kb script from CherryPy to Apache. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
What about setting the "server.threadPool" option under "[Global]" in the configuration file to, say, 10 or 20? That should get CherryPy using threads.
|
|
|
|
|
|
#3 |
|
Banned
![]() ![]() |
Okay, wow. I'm not going to test it yet. First I will need a way to synchronize the data files so one doesn't write while the other is reading, etc...
Errm... so how is that possible? @_@ |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Well, the first solution that springs to mind is to create a lock file, but since only your cherrypy server is supposed to be accessing these files, I suspect you could get away with a Lock object.
import threading
import cherrypy
import datetime
lock = threading.Lock()
class HelloWorld:
@cherrypy.expose
def index(self):
lock.acquire()
file = open("test.txt", "a")
file.write(str(datetime.datetime.now()) + "\n")
file.close()
lock.release()
cherrypy.root = HelloWorld()
cherrypy.server.start() |
|
|
|
|
|
#5 | |
|
Banned
![]() ![]() |
That is seriously the coolest thing I've ever seen. @_@
lock.acquire() / lock.release() .... *drools* Thanks man. ^_^ Quote:
Just curious though. If one section of code is locked using lock.acquire, can a completely different section containing lock.acquire be accessed? Or is it smart enough to map the code blocks to seperate instances? Oh... but how would I make it so code that's writing to a file in a certain place won't be doing it at the same time it's being written. Or should I not worry about synchronizing something that's just reading? But even so I have more then two different pages that will write to the same file. ![]() Last edited by Sane; Feb 18th, 2006 at 9:06 PM. |
|
|
|
|
|
|
#6 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
The pseudocode of what needs to be done is: 1. A file can be written if not being already read or written. 2. A file can be read if not already being written. My first thoughts were that you need a lock for each file. A cache seems the best option. Luckily I never leave home without my automated cache maker function: def cached(func):
"""Creates a cached function.
Cached function cannot handle keyword or mutable arguments."""
store = {}
def cached_func(*args):
if not store.has_key(args):
store[args] = func(*args)
return store[args]
return cached_funcimport threading
@cached
def FileLock(key):
return threading.Lock()class ReverseSemaphore:
def __init__(self):
self.__cond = threading.Condition(threading.Lock())
self.__value = 0
def acquire(self, blocking = True):
is_blocked = False
self.__cond.acquire()
while self.__value > 0:
if not blocking:
break
self.__cond.wait()
else:
self.__value += 1
is_blocked = True
self.__cond.release()
return is_blocked
def release(self):
self.__cond.acquire()
self.__value -= 1
self.__cond.notify()
self.__cond.release()There has to be a simpler solution. I'll update this thread when I have anything more. |
||
|
|
|
|
|
#7 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#8 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Currently trying to fix this Python cookbook recipe. The author has the right idea, but as the comment points out, the code will lock up under some circumstances. Namely, if the program tries to acquire a write-lock before all the read-locks have been released.
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Combining the ReadWriteLock class with a cache: @cached
def FileReadWriteLock(filename):
return ReadWriteLock()
lock = FileReadWriteLock("test.txt")
lock.acquire_write()
file = open("test.txt", "w")
file.write("Hello world\n")
file.close()
lock.release_write()
lock.acquire_read()
file = open("test.txt", "r")
print file.readline()
file.close()
lock.release_read() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|