ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/README
(Generate patch)

Comparing Coro-Mysql/README (file contents):
Revision 1.2 by root, Sat May 30 06:58:22 2009 UTC vs.
Revision 1.4 by root, Sun Feb 20 10:35:10 2011 UTC

16 16
17 This can be used to make parallel sql requests using Coro, or to do 17 This can be used to make parallel sql requests using Coro, or to do
18 other stuff while mysql is rumbling in the background. 18 other stuff while mysql is rumbling in the background.
19 19
20 CAVEAT 20 CAVEAT
21 Note that this module must be linked against exactly the same 21 Note that this module must be linked against exactly the same (shared,
22 possibly not working with all OSes) libmysqlclient library as
22 libmysqlclient library as DBD::mysql, otherwise it will not work. 23 DBD::mysql, otherwise it will not work.
23 24
24 Also, while this module makes database handles non-blocking, you still 25 Also, while this module makes database handles non-blocking, you still
25 cannot run multiple requests in parallel on the same database handle. If 26 cannot run multiple requests in parallel on the same database handle. If
26 you want to run multiple queries in parallel, you have to create 27 you want to run multiple queries in parallel, you have to create
27 multiple database connections, one for each thread that runs queries. 28 multiple database connections, one for each thread that runs queries.
29 Not doing so can corrupt your data - use a Coro::Semaphore when in
30 doubt.
28 31
29 If you make sure that you never run two or more requests in parallel, 32 If you make sure that you never run two or more requests in parallel,
30 you cna freely share the database handles between threads, of course. 33 you can freely share the database handles between threads, of course.
31 34
32 Also, this module uses a number of "unclean" techniques (patching an 35 Also, this module uses a number of "unclean" techniques (patching an
33 internal libmysql structure for one thing) and was hacked within a few 36 internal libmysql structure for one thing) and was initially hacked
34 hours on a long flight to Malaysia. 37 within a few hours on a long flight to Malaysia.
35 38
36 It does, however, check whether it indeed got the structure layout 39 It does, however, check whether it indeed got the structure layout
37 correct, so you should expect perl exceptions or early crashes as 40 correct, so you should expect perl exceptions or early crashes as
38 opposed to data corruption when something goes wrong. 41 opposed to data corruption when something goes wrong during patching.
39 42
40 SPEED 43 SPEED
41 This module is implemented in XS, and as long as mysqld replies quickly 44 This module is implemented in XS, and as long as mysqld replies quickly
42 enough, it adds no overhead to the standard libmysql communication 45 enough, it adds no overhead to the standard libmysql communication
43 routines (which are very badly written). 46 routines (which are very badly written, btw.). In fact, since it has a
47 more efficient buffering and allows requests to run in parallel, it
48 often decreases the actual time to run many queries considerably.
44 49
45 For very fast queries ("select 0"), this module can add noticable 50 For very fast queries ("select 0"), this module can add noticable
46 overhead (around 15%) as it tries to switch to other coroutines when 51 overhead (around 15%, 7% when EV can be used) as it tries to switch to
47 mysqld doesn't deliver the data instantly. 52 other coroutines when mysqld doesn't deliver the data immediately,
53 although, again, when running queries in parallel, they will usually
54 execute faster.
48 55
49 For most types of queries, there will be no overhead, especially on 56 For most types of queries, there will be no extra latency, especially on
50 multicore systems where your perl process can do other things while 57 multicore systems where your perl process can do other things while
51 mysqld does its stuff. 58 mysqld does its stuff.
59
60 LIMITATIONS
61 This module only supports "standard" mysql connection handles - this
62 means unix domain or TCP sockets, and excludes SSL/TLS connections,
63 named pipes (windows) and shared memory (also windows). No support for
64 these connection types is planned, either.
65
66CANCELLATION
67 Cancelling a thread that is within a mysql query will likely make the
68 handle unusable. As far as Coro::Mysql is concerned, the handle can be
69 safely destroyed, but it's not clear how mysql itself will react to a
70 cancellation.
71
72FUNCTIONS
73 Coro::Mysql offers a single user-accessible function:
52 74
53 $DBH = Coro::Mysql::unblock $DBH 75 $DBH = Coro::Mysql::unblock $DBH
54 This function takes a DBI database handles and "patches" it so it 76 This function takes a DBI database handles and "patches" it so it
55 becomes compatible to Coro threads. 77 becomes compatible to Coro threads.
56 78
57 After that, it returns the patched handle - you should always use 79 After that, it returns the patched handle - you should always use
58 the newly returned database handle. 80 the newly returned database handle.
59 81
82 It is safe to call this function on any database handle (or just
83 about any value), but it will only do anything to DBD::mysql
84 handles, others are returned unchanged. That means it is harmless
85 when applied to database handles of other databases.
86
87 It is also safe to pass "undef", so code like this is works as
88 expected:
89
90 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
91 or die $DBI::errstr;
92
93USAGE EXAMPLE
94 This example uses PApp::SQL and Coro::on_enter to implement a function
95 "with_db", that connects to a database, uses "unblock" on the resulting
96 handle and then makes sure that $PApp::SQL::DBH is set to the
97 (per-thread) database handle when the given thread is running (it does
98 not restore any previous value of $PApp::SQL::DBH, however):
99
100 use Coro;
101 use Coro::Mysql;
102 use PApp::SQL;
103
104 sub with_db($$$&) {
105 my ($database, $user, $pass, $cb) = @_;
106
107 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
108 or die $DBI::errstr;
109
110 Coro::on_enter { $PApp::SQL::DBH = $dbh };
111
112 $cb->();
113 }
114
115 This function makes it possible to easily use PApp::SQL with
116 Coro::Mysql, without worrying about database handles.
117
118 # now start 10 threads doing stuff
119 async {
120
121 with_db "DBI:mysql:test", "", "", sub {
122 sql_exec "update table set col = 5 where id = 7";
123
124 my $st = sql_exec \my ($id, $name),
125 "select id, name from table where name like ?",
126 "a%";
127
128 while ($st->fetch) {
129 ...
130 }
131
132 my $id = sql_insertid sql_exec "insert into table values (1,2,3)";
133 # etc.
134 };
135
136 } for 1..10;
137
138SEE ALSO
139 Coro, PApp::SQL (a user friendly but efficient wrapper around DBI).
140
60AUTHOR 141AUTHOR
61 Marc Lehmann <schmorp@schmorp.de> 142 Marc Lehmann <schmorp@schmorp.de>
62 http://home.schmorp.de/ 143 http://home.schmorp.de/
63 144

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines