Project

General

Profile

Any example on how to tap onto the Wt event loop?

Added by Vincenzo Romano about 13 years ago

I'm developing an application which will use PostgreSQL asynchronous query I/O.

Unless I've missed some documentation page (which cannot be that hard) I've not found details on how to do it.

Is there any reference to the documentation and/or, even better, a real world example?

By the way, the sourceforge download page is again misaligned with the real "latest release".


Replies (3)

RE: Any example on how to tap onto the Wt event loop? - Added by Koen Deforche about 13 years ago

Hey,

No, we do not support the asynchronous query I/O interface of postgres. In fact, I was not aware that existed ?

Thanks for the sourceforge tip, we were not aware.

Regards,

koen

RE: Any example on how to tap onto the Wt event loop? - Added by Vincenzo Romano about 13 years ago

Maybe I've been unclear. I am not planning to use Wt:Dbo (as I have already my classes).

What I would like to do is to apply this:

(quoting from different libpq documentation pages)

If PQconnectStart succeeds, the next stage is to poll libpq so that it can proceed with the connection sequence. Use PQsocket(conn) to obtain the descriptor of the socket underlying the database connection. Loop thus: If PQconnectPoll(conn) last returned PGRES_POLLING_READING, wait until the socket is ready to read (as indicated by select(), poll(), or similar system function). Then call PQconnectPoll(conn) again. Conversely, if PQconnectPoll(conn) last returned PGRES_POLLING_WRITING, wait until the socket is ready to write, then call PQconnectPoll(conn) again. If you have yet to call PQconnectPoll, i.e., just after the call to PQconnectStart, behave as if it last returned PGRES_POLLING_WRITING. Continue this loop until PQconnectPoll(conn) returns PGRES_POLLING_FAILED, indicating the connection procedure has failed, or PGRES_POLLING_OK, indicating the connection has been successfully made.

...

A typical application using these functions will have a main loop that uses select() or poll() to wait for all the conditions that it must respond to. One of the conditions will be input available from the server, which in terms of select() means readable data on the file descriptor identified by PQsocket. When the main loop detects input ready, it should call PQconsumeInput to read the input. It can then call PQisBusy, followed by PQgetResult if PQisBusy returns false (0). It can also call PQnotifies to detect NOTIFY messages (see Section 31.7).

So what I'd like to do (provided that it's actually possible) is to expolit Wt to hook my PGSql classes to handle the asynchronous I/O.

Is it possible?

RE: Any example on how to tap onto the Wt event loop? - Added by Koen Deforche about 13 years ago

Hey,

Well, the easiest way to do this is to create your own event loop (using a single thread) that does the select() or poll() for postgres.

From within your wt applications, it is then possible to use WApplication::deferRendering() and resumeRendering() to wait for an asynchronous event without blocking a (Wt event loop) thread. This is typically used now in conjunction with the asynchronous HTTP client that comes with Wt. See for example Wt::Auth::FacebookService which uses this mechanism while waiting for a response from facebook.

To hook into Wt's event loop would require you to integrate with Boost.Asio, since Wt uses boost asio as reactor implementation. I don't think it's worth it, a single thread dedicated to a Postgres-reactor seems entirely warranted and much more simple.

Regards,

koen

    (1-3/3)