ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/Mysql.pm
Revision: 1.1
Committed: Mon May 18 15:19:38 2009 UTC (15 years ago) by root
Branch: MAIN
Log Message:
initial check-in

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.01';
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 =cut
95
96 sub unblock {
97 my ($DBH) = @_;
98 my $sock = $DBH->{sock};
99
100 open my $fh, "+>&" . $DBH->{sockfd}
101 or croak "Coro::Mysql unable to clone mysql fd";
102
103 $fh = Coro::Handle::unblock $fh;
104
105 _patch $sock, $DBH->{sockfd}, tied ${$fh};
106 $DBH->{private_Coro_Mysql} = guard {
107 _unpatch $sock;
108 undef $fh;
109 };
110
111 $DBH
112 }
113
114 1;
115
116 =back
117
118 =head1 AUTHOR
119
120 Marc Lehmann <schmorp@schmorp.de>
121 http://home.schmorp.de/
122
123 =cut
124