ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/Mysql.pm
Revision: 1.3
Committed: Mon Jun 1 12:03:21 2009 UTC (15 years, 1 month ago) by root
Branch: MAIN
Changes since 1.2: +16 -10 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::Mysql - let other threads run while doing mysql requests
4
5 =head1 SYNOPSIS
6
7 use Coro::Mysql;
8
9 my $DBH = Coro::Mysql::unblock DBI->connect (...);
10
11 =head1 DESCRIPTION
12
13 (Note that in this manual, "thread" refers to real threads as implemented
14 by the Coro module, not to the built-in windows process emulation which
15 unfortunately is also called "threads")
16
17 This module "patches" DBD::mysql database handles so that they do not
18 block the whole process, but only the thread that they are used in.
19
20 This can be used to make parallel sql requests using Coro, or to do other
21 stuff while mysql is rumbling in the background.
22
23 =head2 CAVEAT
24
25 Note that this module must be linked against exactly the same
26 F<libmysqlclient> library as DBD::mysql, otherwise it will not work.
27
28 Also, while this module makes database handles non-blocking, you still
29 cannot run multiple requests in parallel on the same database handle. If
30 you want to run multiple queries in parallel, you have to create multiple
31 database connections, one for each thread that runs queries.
32
33 If you make sure that you never run two or more requests in parallel, you
34 cna freely share the database handles between threads, of course.
35
36 Also, this module uses a number of "unclean" techniques (patching an
37 internal libmysql structure for one thing) and was hacked within a few
38 hours on a long flight to Malaysia.
39
40 It does, however, check whether it indeed got the structure layout
41 correct, so you should expect perl exceptions or early crashes as opposed
42 to data corruption when something goes wrong.
43
44 =head2 SPEED
45
46 This module is implemented in XS, and as long as mysqld replies quickly
47 enough, it adds no overhead to the standard libmysql communication
48 routines (which are very badly written).
49
50 For very fast queries ("select 0"), this module can add noticable overhead
51 (around 15%) as it tries to switch to other coroutines when mysqld doesn't
52 deliver the data instantly.
53
54 For most types of queries, there will be no overhead, especially on
55 multicore systems where your perl process can do other things while mysqld
56 does its stuff.
57
58 =over 4
59
60 =cut
61
62 package Coro::Mysql;
63
64 use strict qw(vars subs);
65 no warnings;
66
67 use Scalar::Util ();
68 use Carp qw(croak);
69
70 use Guard;
71 use Coro::Handle ();
72
73 # we need this extra indirection, as Coro doesn't support
74 # calling SLF-like functions via call_sv.
75
76 sub readable { &Coro::Handle::FH::readable }
77 sub writable { &Coro::Handle::FH::writable }
78
79 BEGIN {
80 our $VERSION = '0.2';
81
82 require XSLoader;
83 XSLoader::load Coro::Mysql::, $VERSION;
84 }
85
86 =item $DBH = Coro::Mysql::unblock $DBH
87
88 This function takes a DBI database handles and "patches" it
89 so it becomes compatible to Coro threads.
90
91 After that, it returns the patched handle - you should always use the
92 newly returned database handle.
93
94 It is safe to call this function on any database handle, but it will only
95 do anything to L<DBD::mysql> handles, others are returned unchanged.
96
97 =cut
98
99 sub unblock {
100 my ($DBH) = @_;
101
102 if ($DBH->{Driver}{Name} eq "mysql") {
103 my $sock = $DBH->{sock};
104
105 open my $fh, "+>&" . $DBH->{sockfd}
106 or croak "Coro::Mysql unable to clone mysql fd";
107
108 $fh = Coro::Handle::unblock $fh;
109
110 _patch $sock, $DBH->{sockfd}, tied ${$fh};
111 $DBH->{private_Coro_Mysql} = guard {
112 _unpatch $sock;
113 undef $fh;
114 };
115 }
116
117 $DBH
118 }
119
120 1;
121
122 =back
123
124 =head1 AUTHOR
125
126 Marc Lehmann <schmorp@schmorp.de>
127 http://home.schmorp.de/
128
129 =cut
130