The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Share::Threads

This module shows how one can share http cookies and dns cache between multiple threads.

Motivation

Threads are evil, but some people think they are not. I want to make them a favor and show how bad threads really are.

Limitations

  • Net::Curl::Share is the only package that allows sharing between threads. Others (Easy, Multi, Form) are usable only in their creating thread.

  • Share internals are always shared between threads, but you must mark your base object as shared if you want to use the data elsewhere.

  • Shared Net::Curl::Share does not support lock and unlock callbacks. However, locking is done internally, so no worries about corruption.

  • If we want to share the data, we cannot trigger all downloads at the same time, because there would be no data to share at the time. This solution opts to lock other downloads until headers from the server are fully received. It assures cache coherency, but slows down overall application.

  • This method does not reuse persistent connections, it would be much faster to get those 6 requests one after another than to doing all 6 in parallel.

  • If you share dns cache all connections for one domain will go to the same IP, even if domain name resolves to multiple adresses.

MODULE CODE

TEST Easy package

This Easy::Threads object will block whole share object for duration of dns name resolution and until headers are completely received.

TEST APPLICATION

Sample application using this module looks like this:

        #!perl
        use threads;
        use threads::shared;
        use strict;
        use warnings;
        use Share::Threads;
        use Easy::Threads;
#nopod