Martijn Lievaart <> wrote:
>
> >> I want to make the server concurrent by using ithreads,
> >> creating n threads and distributing the requests over the threads
> >> using Threads::Queue. The thread itself should send back the result to
> >> the client.
> >
> > Why not just use another Thread::Queue to put the answer in, and have
> > the main thread send all the responses?
>
> Because I don't see how I can both select() and dequeue() in the main
> thread. Or do you see another solution?
You could open a pipe from socket to itself. Have the slaves print one byte
to it when they have just enqueued something. Then use select on both the
accept handle and on the semaphor handle--when you get the semaphor thread
you know you should check the queue.
>
> I thought of another solution. I could use fork instead of threads and
> communicate over sockets or pipes. That way I can simply use select in
> the main thread to get both new requests and answers.
Yes, or that. For some reason I like using the pipe just as a semaphor.
I guess because with Thread::Queue your messages are self-delimited, so
you don't have to screw around with that.
> This has another advantage. It makes signal handling much simpler. I
> think signals and threads cannot be used (simple) together so this is
> probably a better solution overall.
>
> The problem here is that I also want some kind of caching, that is why I
> looked at threads in the first place. Oh well, I can use an on disk cache
> as well.
Checking the cache should be fast, right? With the forking method, you
could only fork if the thing wasn't in the cache, and then use a pipe so
the child can tell the parent how to update it's cache. Of course, if your
cache is going to be (or likely become in the forseeable future) bigger
than memory, then there is no reason to go to lengths to avoid a disk
cache.
> >> Now I understand the basics of :shared. I just cannot wrap this around
> >> to sockets.
> >>
> >> For TCP: Can I just enqueue a socket and use it in the thread that
> >> dequeues it?
> >
> > I don't think so. You can't enqueue objects. You could enqueue some
> > kind
>
> I could not find that in the docs, do you have a pointer?
Mostly I just got it from the error messages. Actually, you can enqueue
objects, as long as they are "shared" before you try to enqueue them. But
sharing objects isn't necessarily well supported, especially file handles,
it seems
>
> > of index or key that could be used to point to the socket residing in
> > some other variable.
>
> Yes. Of course. Slaps forehead.
Actually, I'm not sure how this would work, either. See below.
> >> Or will this not work
> >> at all? Or depending on the OS (I'm using Linux).
> >
> > In my experience on linux, file handles (including sockets) don't need
> > to be shared. When you create a new thread, a new Perl variable is
> > created for the handle, but it points to the same underlying C/OS
> > structure as the old one, so the socket is implicitly shared by all
> > threads which are "downstream" from the thread that initiated the
> > socket. You may need to create a shared (and hence lockable) sentinel
> > variable to control access to the real socket, if such serialized
> > control is necessary.
However, this is all irrelevant to you. Since you are using pre-created
threads, the relevant sockets will not exist at the time of thread
creation, and so won't be shared through this automatic method. Maybe you
can use something like File::FDpasser (I've never used it) to do it, or
just get the file descriptor number using fileno and enqueue that, then
reconstruct it at the end (I've never done that, either.)
> Slaps forehead again. Of course. I should not have searched the perl docs
> but the Linux docs.
I don't this is documented there, either, as it is a result of the an
interaction between how linux implements files and how Perl implements
threads. My ideas in this area are mostly based on experience and
experiments, not on the docs.
Xho
--
--------------------
http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB