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

Comparing Coro-Mysql/Mysql.pm (file contents):
Revision 1.11 by root, Sun Feb 20 10:35:10 2011 UTC vs.
Revision 1.23 by root, Mon Mar 4 05:44:43 2019 UTC

1=head1 NAME 1=head1 NAME
2 2
3Coro::Mysql - let other threads run while doing mysql requests 3Coro::Mysql - let other threads run while doing mysql/mariadb requests
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Coro::Mysql; 7 use Coro::Mysql;
8 8
12 12
13(Note that in this manual, "thread" refers to real threads as implemented 13(Note that in this manual, "thread" refers to real threads as implemented
14by the Coro module, not to the built-in windows process emulation which 14by the Coro module, not to the built-in windows process emulation which
15unfortunately is also called "threads") 15unfortunately is also called "threads")
16 16
17This module "patches" DBD::mysql database handles so that they do not 17This module replaces the I/O handlers for a database connection, with the
18block the whole process, but only the thread that they are used in. 18effect that "patched" database handles no longer block the all threads of
19a process, but only the thread that does the request.
19 20
20This can be used to make parallel sql requests using Coro, or to do other 21This can be used to make parallel sql requests using Coro, or to do other
21stuff while mysql is rumbling in the background. 22stuff while mariadb is rumbling in the background.
22 23
23=head2 CAVEAT 24=head2 CAVEAT
24 25
25Note that this module must be linked against exactly the same (shared, 26Note that this module must be linked against exactly the same (shared,
26possibly not working with all OSes) F<libmysqlclient> library as 27possibly not working with all OSes) F<libmariadb>/F<libmysqlclient>
27DBD::mysql, otherwise it will not work. 28library as L<DBD::mysql>, otherwise it will not work.
28 29
29Also, while this module makes database handles non-blocking, you still 30Also, while this module makes database handles non-blocking, you still
30cannot run multiple requests in parallel on the same database handle. If 31cannot run multiple requests in parallel on the same database handle. If
31you want to run multiple queries in parallel, you have to create multiple 32you want to run multiple queries in parallel, you have to create multiple
32database connections, one for each thread that runs queries. Not doing so 33database connections, one for each thread that runs queries. Not doing
33can corrupt your data - use a Coro::Semaphore when in doubt. 34so can corrupt your data - use a Coro::Semaphore to protetc access to a
35shared database handle when in doubt.
34 36
35If you make sure that you never run two or more requests in parallel, you 37If you make sure that you never run two or more requests in parallel, you
36can freely share the database handles between threads, of course. 38can freely share the database handles between threads, of course.
37
38Also, this module uses a number of "unclean" techniques (patching an
39internal libmysql structure for one thing) and was initially hacked within
40a few hours on a long flight to Malaysia.
41
42It does, however, check whether it indeed got the structure layout
43correct, so you should expect perl exceptions or early crashes as opposed
44to data corruption when something goes wrong during patching.
45 39
46=head2 SPEED 40=head2 SPEED
47 41
48This module is implemented in XS, and as long as mysqld replies quickly 42This module is implemented in XS, and as long as mysqld replies quickly
49enough, it adds no overhead to the standard libmysql communication 43enough, it adds no overhead to the standard libmysql communication
100 94
101sub readable { &Coro::Handle::FH::readable } 95sub readable { &Coro::Handle::FH::readable }
102sub writable { &Coro::Handle::FH::writable } 96sub writable { &Coro::Handle::FH::writable }
103 97
104BEGIN { 98BEGIN {
105 our $VERSION = '1.1'; 99 our $VERSION = '2.0';
106 100
107 require XSLoader; 101 require XSLoader;
108 XSLoader::load Coro::Mysql::, $VERSION; 102 XSLoader::load Coro::Mysql::, $VERSION;
109} 103}
110 104
140 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { 134 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) {
141 require Coro::Handle; 135 require Coro::Handle;
142 $fh = Coro::Handle::unblock ($fh); 136 $fh = Coro::Handle::unblock ($fh);
143 } 137 }
144 138
145 _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; 139 _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied *$$fh;
146 } 140 }
147 141
148 $DBH 142 $DBH
149} 143}
150 144
171 or die $DBI::errstr; 165 or die $DBI::errstr;
172 166
173 Coro::on_enter { $PApp::SQL::DBH = $dbh }; 167 Coro::on_enter { $PApp::SQL::DBH = $dbh };
174 168
175 $cb->(); 169 $cb->();
176 } 170 }
177 171
178This function makes it possible to easily use L<PApp::SQL> with 172This function makes it possible to easily use L<PApp::SQL> with
179L<Coro::Mysql>, without worrying about database handles. 173L<Coro::Mysql>, without worrying about database handles.
180 174
181 # now start 10 threads doing stuff 175 # now start 10 threads doing stuff
200 194
201=head1 SEE ALSO 195=head1 SEE ALSO
202 196
203L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). 197L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI).
204 198
199=head1 HISTORY
200
201This module was initially hacked together within a few hours on a long
202flight to Malaysia, and seems to have worked ever since, with minor
203adjustments for newer libmysqlclient libraries.
204
205Well, at least until mariadb introduced the new Pluggable Virtual IO API
206in mariadb 10.3, which changed and broke everything. On the positive
207side, the old system was horrible to use, as many GNU/Linux distributions
208forgot to include the required heaqder files and there were frequent small
209changes, while the new PVIO system seems to be "official" and hopefully
210better supported.
211
205=head1 AUTHOR 212=head1 AUTHOR
206 213
207 Marc Lehmann <schmorp@schmorp.de> 214 Marc Lehmann <schmorp@schmorp.de>
208 http://home.schmorp.de/ 215 http://home.schmorp.de/
209 216

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines