1 | NAME |
1 | NAME |
2 | Coro::Mysql - let other threads run while doing mysql requests |
2 | Coro::Mysql - let other threads run while doing mysql/mariadb requests |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use Coro::Mysql; |
5 | use Coro::Mysql; |
6 | |
6 | |
7 | my $DBH = Coro::Mysql::unblock DBI->connect (...); |
7 | my $DBH = Coro::Mysql::unblock DBI->connect (...); |
8 | |
8 | |
9 | DESCRIPTION |
9 | DESCRIPTION |
10 | (Note that in this manual, "thread" refers to real threads as |
10 | (Note that in this manual, "thread" refers to real threads as |
11 | implemented by the Coro module, not to the built-in windows process |
11 | implemented by the Coro module, not to the built-in windows process |
12 | emulation which unfortunately is also called "threads") |
12 | emulation which unfortunately is also called "threads"). |
13 | |
13 | |
14 | This module replaces the I/O handlers for a database connection, with |
14 | This module replaces the I/O handlers for a database connection, with |
15 | the effect that "patched" database handles no longer block the all |
15 | the effect that "patched" database handles no longer block all threads |
16 | threads of a process, but only the thread that does the request. |
16 | of a process, but only the thread that does the request. It should work |
|
|
17 | for both DBD::mysql and DBD::MariaDB connections and a wide range of |
|
|
18 | mariadb/mysql client libraries. |
17 | |
19 | |
18 | This can be used to make parallel sql requests using Coro, or to do |
20 | This can be used to make parallel sql requests using Coro, or to do |
19 | other stuff while mysql is rumbling in the background. |
21 | other stuff while mariadb is rumbling in the background. |
20 | |
22 | |
21 | CAVEAT |
23 | CAVEAT |
22 | Note that this module must be linked against exactly the same (shared, |
24 | Note that this module must be linked against exactly the same (shared, |
23 | possibly not working with all OSes) libmysqlclient library as |
25 | possibly not working with all OSes) libmariadb/libmysqlclient library as |
24 | DBD::mysql, otherwise it will not work. |
26 | DBD::MariaDB/DBD::mysql, otherwise it will not work. |
25 | |
|
|
26 | Also, this module requires a header file that apparently isn't installed |
|
|
27 | everywhere (violite.h), and therefore comes with it's own copy, which |
|
|
28 | might or might not be compatible to the violite.h of your library - when |
|
|
29 | in doubt, make sure all the libmysqlclient header files are installed |
|
|
30 | and delete the violite.h header that comes with this module. |
|
|
31 | |
|
|
32 | On the good side, this module does a multitude of checks to ensure that |
|
|
33 | the libray versions match on the binary level, so on incompatibilities |
|
|
34 | you should expect an exception when trying to unblock a handle, rather |
|
|
35 | than data corruption. |
|
|
36 | |
27 | |
37 | Also, while this module makes database handles non-blocking, you still |
28 | Also, while this module makes database handles non-blocking, you still |
38 | cannot run multiple requests in parallel on the same database handle. If |
29 | cannot run multiple requests in parallel on the same database handle. If |
39 | you want to run multiple queries in parallel, you have to create |
30 | you want to run multiple queries in parallel, you have to create |
40 | multiple database connections, one for each thread that runs queries. |
31 | multiple database connections, one for each thread that runs queries. |
… | |
… | |
72 | handle unusable. As far as Coro::Mysql is concerned, the handle can be |
63 | handle unusable. As far as Coro::Mysql is concerned, the handle can be |
73 | safely destroyed, but it's not clear how mysql itself will react to a |
64 | safely destroyed, but it's not clear how mysql itself will react to a |
74 | cancellation. |
65 | cancellation. |
75 | |
66 | |
76 | FUNCTIONS |
67 | FUNCTIONS |
77 | Coro::Mysql offers a single user-accessible function: |
68 | Coro::Mysql offers these functions, the only one that oyu usually need |
|
|
69 | is "unblock": |
78 | |
70 | |
79 | $DBH = Coro::Mysql::unblock $DBH |
71 | $DBH = Coro::Mysql::unblock $DBH |
80 | This function takes a DBI database handles and "patches" it so it |
72 | This function takes a DBI database handles and "patches" it so it |
81 | becomes compatible to Coro threads. |
73 | becomes compatible to Coro threads. |
82 | |
74 | |
… | |
… | |
91 | It is also safe to pass "undef", so code like this is works as |
83 | It is also safe to pass "undef", so code like this is works as |
92 | expected: |
84 | expected: |
93 | |
85 | |
94 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
86 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
95 | or die $DBI::errstr; |
87 | or die $DBI::errstr; |
|
|
88 | |
|
|
89 | $bool = Coro::Mysql::is_unblocked $DBH |
|
|
90 | Returns true iff the database handle was successfully patched for |
|
|
91 | non-blocking operations. |
|
|
92 | |
|
|
93 | $bool = Coro::Mysql::have_ev |
|
|
94 | Returns true if this Coro::Mysql installation is compiled with |
|
|
95 | special support for EV or not. |
|
|
96 | |
|
|
97 | Even if compiled in, it will only be used if EV is actually the |
|
|
98 | AnyEvent event backend. |
96 | |
99 | |
97 | USAGE EXAMPLE |
100 | USAGE EXAMPLE |
98 | This example uses PApp::SQL and Coro::on_enter to implement a function |
101 | This example uses PApp::SQL and Coro::on_enter to implement a function |
99 | "with_db", that connects to a database, uses "unblock" on the resulting |
102 | "with_db", that connects to a database, uses "unblock" on the resulting |
100 | handle and then makes sure that $PApp::SQL::DBH is set to the |
103 | handle and then makes sure that $PApp::SQL::DBH is set to the |
… | |
… | |
145 | HISTORY |
148 | HISTORY |
146 | This module was initially hacked together within a few hours on a long |
149 | This module was initially hacked together within a few hours on a long |
147 | flight to Malaysia, and seems to have worked ever since, with minor |
150 | flight to Malaysia, and seems to have worked ever since, with minor |
148 | adjustments for newer libmysqlclient libraries. |
151 | adjustments for newer libmysqlclient libraries. |
149 | |
152 | |
|
|
153 | Well, at least until mariadb introduced the new Pluggable Virtual IO API |
|
|
154 | in mariadb 10.3, which changed and broke everything. On the positive |
|
|
155 | side, the old system was horrible to use, as many GNU/Linux |
|
|
156 | distributions forgot to include the required heaqder files and there |
|
|
157 | were frequent small changes, while the new PVIO system seems to be |
|
|
158 | "official" and hopefully better supported. |
|
|
159 | |
150 | AUTHOR |
160 | AUTHOR |
151 | Marc Lehmann <schmorp@schmorp.de> |
161 | Marc Lehmann <schmorp@schmorp.de> |
152 | http://home.schmorp.de/ |
162 | http://home.schmorp.de/ |
153 | |
163 | |