我可以在 Perl 的 DBI 和 Oracle 中使用多线程吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/370425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use multithreading with Perl's DBI and Oracle?
提问by TM.
Does anyone know of any gotachs or problems when writing multithreaded Perl applications using the Oracle DBI? Each thread would have it's own connection to Oracle.
有谁知道在使用 Oracle DBI 编写多线程 Perl 应用程序时有任何问题或问题吗?每个线程都有自己的与 Oracle 的连接。
For the longest time I was told multithreading was not supported in Perl with Oracle.
在最长的时间里,我被告知使用 Oracle 的 Perl 不支持多线程。
回答by Jonathan Leffler
The Perl DBI enforces single-threading through its interior, so the drivers will only be active on one session ($dbh
) at a time. Regardless of how many CPUs you have. So, multi-threading is not supported (because everything inside DBI is single-threaded), but it is safe to use DBI (and hence DBD::Oracle) in a multi-threaded application.
Perl DBI 通过其内部强制执行单线程,因此驱动程序一次只能在一个会话 ( $dbh
) 上处于活动状态。不管你有多少 CPU。因此,不支持多线程(因为 DBI 中的所有内容都是单线程的),但在多线程应用程序中使用 DBI(以及 DBD::Oracle)是安全的。
回答by brian d foy
Well, the DBI documentation says not to use a threaded Perl, and points to a Perlmonks post that explains that. The documentation telling you not to do it is a pretty good reason.
好吧,DBI 文档说不要使用线程化 Perl,并指向Perlmonks 的一篇文章,解释了. 文档告诉你不要这样做是一个很好的理由。
However, I've seen it work just fine on some platforms but fail miserably on others. It's certainly not portable even if you do get it to work.
但是,我已经看到它在某些平台上运行良好,但在其他平台上却惨遭失败。即使您确实可以使用它,它也肯定不是便携式的。
回答by brian d foy
I've used multi threading in Perl for benchmarking an Oracle db and I had to create a database handler for every thread.
我在 Perl 中使用了多线程来对 Oracle 数据库进行基准测试,并且必须为每个线程创建一个数据库处理程序。
回答by Ape-inago
A while backI mannaged to get an ad-hock implementation working... I treated the connection w/ dbi as a limited resource and shared it among the various threads in Perl using a file locking mechanism. My multi-threaded app only ever connected to the dbi through a seperate perl-script running as a daemon.
前阵子我mannaged获得一个广告典当实施工作......我视为一个有限的资源进行连接W / DBI和共享它使用文件锁定机制,在Perl各个线程之间。我的多线程应用程序仅通过作为守护程序运行的单独 perl 脚本连接到 dbi。
On Linux, the multithreaded end was done via fork, on windows I used whatever came with the default activeperl implementation ( I forget )
在 Linux 上,多线程结束是通过 fork 完成的,在 Windows 上我使用了默认的 activeperl 实现(我忘记了)
I tried having them communicate via shared memory, but ended up just using a shared file instead. Linux has reliable append mode, so it was a piece of cake. On windows it was much more difficult to get them synchronized.
我尝试让它们通过共享内存进行通信,但最终只使用了共享文件。Linux 有可靠的追加模式,所以它是小菜一碟。在 Windows 上,让它们同步要困难得多。
RecentlyI've looked into database transactions, with each instance of the thread having it's own connection to the database, and letting the database handle the connection details.
最近我研究了数据库事务,线程的每个实例都有自己的数据库连接,并让数据库处理连接细节。
This is with mysql, but i'm sure oracle supports transactions.
这是用 mysql,但我确定 oracle 支持事务。
apache::dbi works/plays well with mod_perl in keeping these connections alive between each run-through of the script (before I used this, each connection made was quite time consuming).
apache::dbi 与 mod_perl 一起工作/发挥良好,以保持这些连接在脚本的每次运行之间保持活动状态(在我使用它之前,建立的每个连接都非常耗时)。
Your results will vary.
你的结果会有所不同。
回答by alex
You can try using shell "multithreading" instead. Described in http://alexhanin.blogspot.com/2010/07/multithreading-with-nothing-but-korn.htmlYou can put your perl inside shell wrapper, then use each shell "thread" as a separate instance of a program.
您可以尝试改用 shell 的“多线程”。在http://alexhanin.blogspot.com/2010/07/multithreading-with-nothing-but-korn.html 中描述 您可以将 perl 放在 shell 包装器中,然后将每个 shell“线程”用作程序的单独实例.
回答by MarkR
Yes, using threads in Perl is an extremely bad idea, regardless of whether you use Oracle or not.
是的,在 Perl 中使用线程是一个非常糟糕的主意,无论您是否使用 Oracle。
While in theory, provided each uses its own connection, it should work, perl 5.8 threads are fundamentally flawed.
虽然理论上,如果每个都使用自己的连接,它应该可以工作,perl 5.8 线程从根本上是有缺陷的。
If you understand that article and still want to use Perl threads, good luck.
如果您理解那篇文章并且仍然想使用 Perl 线程,那么祝您好运。