IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

[langage] Aidez-moi avec les threads


Sujet :

Langage Perl

  1. #1
    Membre éclairé
    Avatar de GnuVince
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2004
    Messages
    679
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2004
    Messages : 679
    Points : 803
    Points
    803
    Par défaut [langage] Aidez-moi avec les threads
    Bonjour,

    je voudrais rendre le script suivant threadé pour qu'il exécute un peu plus rapidement lorsqu'un site prend plus de temps à charger. Pouvez-vous m'aider? J'y connais rien dans ce domaine.

    Le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
     
    #!/usr/bin/env perl
     
    use strict;
    use warnings;
     
    use LWP::Simple;
    use POSIX qw(strftime);
     
    # Add or remove the comics you want/don't want.  The comic
    # configurations are kept in an hash.  The key is the name of the
    # comic and the value is an array reference containing the different
    # parameters to get the strip.  The mandatory array values are the
    # following:
    #
    # * The URL where the daily comic is shown
    # * A regular expression that will match the image file
    #
    # Additionally, you can specify an optional third parameter, a "base"
    # URL if the root of the image is different from the URL of the page
    # where we got the image.  If that last element is undefined, the
    # script will assume that the base URL is the same as the image page
    # URL.
    my %strip = (
        "Penny Arcade"      => ["http://www.penny-arcade.com/comic", qr'images/\d{4}/.+(gif|jpg)', "http://www.penny-arcade.com/"],
        "PvP Online"        => ["http://www.pvponline.com/", qr'images/\d{4}\.gif'],
        "User Friendly"     => ["http://userfriendly.org/", qr'cartoons/archives/\w{5}/.+\.gif'],
        "The Flying McCoys" => ["http://www.ucomics.com/theflyingmccoys", qr'fmc/\d{4}/.+\.gif', "http://images.ucomics.com/comics/"],
        "Joy of Tech"       => ["http://www.geekculture.com/joyoftech", qr'\d{3}.?\.(png|gif|jpg)', "http://www.geekculture.com/joyoftech/joyimages/"],
        "Ctrl Alt Del"      => ["http://www.ctrlaltdel-online.com/comic.php", qr'\d{8}\.jpg', "http://www.ctrlaltdel-online.com/comics/"],
        "Foxtrot"           => ["http://www.foxtrot.com", qr'http://images.ucomics.com/comics/ft/\d{4}/.+\.gif', ""],
        "VGCats"            => ["http://www.vgcats.com/comics/", qr'images/\d{6}\..{3}'],
        "Dilbert"           => ["http://www.dilbert.com/", qr'comics/dilbert/archive/images/dilbert\d+\.gif'],
        "Bugbash"           => ["http://www.bugbash.net/", qr'strips/.+gif'],
        "My Extra Life"     => ["http://www.myextralife.com/", qr'strips/.+\.jpg'],
        "Dueling Analogs"   => ["http://www.duelinganalogs.com/", qr'comics/.+\.png'],
        "Little Gamers"     => ["http://www.little-gamers.com/", qr'comics/\d{8}\.jpg'],
        "Frank and Ernest"  => ["http://www.comics.com/comics/franknernest/", qr'archive/images/frank.+(gif|jpg)'],
        "Theater Hopper"    => ["http://www.theaterhopper.com/", qr'vault/\d{6}\.jpg'],
        "The Born Loser"    => ["http://www.comics.com/comics/bornloser/", qr'archive.+bornloser\d+\.gif'],
        "Working Daze"      => ["http://www.comics.com/comics/workingdaze/", qr'archive/images/workingdaze\d+\.(jpg|gif)'],
    );
     
    my $xml_headers = <<'XML';
    <rss version="2.0">
      <channel>
        Comics
        http://gnuvince.net/~vince/comics
        <description>Comics</description>
        <language>en-us</language>
        <ttl>40</ttl>
    XML
     
    my $xml_footers = <<'XML';
      </channel>
    </rss>
    XML
     
    my $str_date = strftime("%a, %d %b %Y %T %Z", localtime);
     
     
     
     
    sub get_strip_url {
        my ($url, $regex, $base) = @_;
        if (!defined $base) {
            $base = $url;
        }
     
        my $src = get($url);
     
        if ($src) {
            my ($file_location) = $src =~ m{($regex)};
            return $base . $file_location if $file_location;
        }
        return "Couldn't fetch strip!";
    }
     
     
    sub print_item {
        my ($name, $link) = @_;
        print <<XML;
        <item>
          $name
          <category>$name</category>
          <description><img src="$link"></description>
          $link
          $str_date
          <guid>$link</guid>
        </item>
    XML
    }
     
     
    sub main {
        # Print the RSS feed
        print $xml_headers;
     
        while (my ($name, $params_ref) = each %strip) {
            my $link = get_strip_url(@$params_ref);
            print_item($name, $link);
        }
     
        print $xml_footers;
    }
     
    main() if !caller;
     
    1;
     
     
    __END__
     
    =head1 NAME
     
    comics
     
    =head1 DESCRIPTION
     
    comics is a simple script that generates an RSS feed with the daily
    strip of several web comics.  comics depends on LWP::Simple which
    can be installed from CPAN to fetch the data necessary to the
    construction of the feed.
     
    =head1 TODO
     
    =over
     
    =item *
     
    Add a configuration file into which people can put the strips they want
     
    =item *
     
    Get more strips in
     
    =back
     
    =head1 AUTHOR
     
    Vincent Foley-Bourgon, <vfoleybourgon at yahoo dot ca>
     
    =head1 COPYRIGHT AND LICENSE
     
    Copyright 2006 by Vincent Foley-Bourgon
     
    This library is free software; you can redistribute it and/or modify
    it under the same terms as Perl itself.
     
    =cut
    Merci bien!

  2. #2
    Membre actif Avatar de scaleo
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    327
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 327
    Points : 219
    Points
    219
    Par défaut
    Les threads perl c'est vraiment la galère j'ai essayer plusieur fois moi même de multithreader des scripts mais j'ai abandonné a chaque, fois.

    Je vais voir si il y a une solution pour le tient , sinon regarde du coté de LWP:arallel::UserAgent
    Vista powaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ::

  3. #3
    Membre actif Avatar de scaleo
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    327
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 327
    Points : 219
    Points
    219
    Par défaut
    Voila une solution qui semble marché elle marche mais avec les threads perl rien n'est garantie sa peut ce mettre a foirer a tout bout de champs sans raison

    J'ai fait un detach pour aller plus vite parce que join est comment dire heu fait partie des choses inutile je ne vois pas l'interet de lancer 1 thread si c'est pour attendre dix plombes qu'il ce finisse



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    #!/usr/bin/env perl
    use strict;
    use threads;
    use warnings;
    use LWP::Simple;
    use POSIX qw(strftime);
     
    # Add or remove the comics you want/don't want.  The comic
    # configurations are kept in an hash.  The key is the name of the
    # comic and the value is an array reference containing the different
    # parameters to get the strip.  The mandatory array values are the
    # following:
    #
    # * The URL where the daily comic is shown
    # * A regular expression that will match the image file
    #
    # Additionally, you can specify an optional third parameter, a "base"
    # URL if the root of the image is different from the URL of the page
    # where we got the image.  If that last element is undefined, the
    # script will assume that the base URL is the same as the image page
    # URL.
    my %strip = (
        "Penny Arcade"      => ["http://www.penny-arcade.com/comic", qr'images/\d{4}/.+(gif|jpg)', "http://www.penny-arcade.com/"],
        "PvP Online"        => ["http://www.pvponline.com/", qr'images/\d{4}\.gif'],
        "User Friendly"     => ["http://userfriendly.org/", qr'cartoons/archives/\w{5}/.+\.gif'],
        "The Flying McCoys" => ["http://www.ucomics.com/theflyingmccoys", qr'fmc/\d{4}/.+\.gif', "http://images.ucomics.com/comics/"],
        "Joy of Tech"       => ["http://www.geekculture.com/joyoftech", qr'\d{3}.?\.(png|gif|jpg)', "http://www.geekculture.com/joyoftech/joyimages/"],
        "Ctrl Alt Del"      => ["http://www.ctrlaltdel-online.com/comic.php", qr'\d{8}\.jpg', "http://www.ctrlaltdel-online.com/comics/"],
        "Foxtrot"           => ["http://www.foxtrot.com", qr'http://images.ucomics.com/comics/ft/\d{4}/.+\.gif', ""],
        "VGCats"            => ["http://www.vgcats.com/comics/", qr'images/\d{6}\..{3}'],
        "Dilbert"           => ["http://www.dilbert.com/", qr'comics/dilbert/archive/images/dilbert\d+\.gif'],
        "Bugbash"           => ["http://www.bugbash.net/", qr'strips/.+gif'],
        "My Extra Life"     => ["http://www.myextralife.com/", qr'strips/.+\.jpg'],
        "Dueling Analogs"   => ["http://www.duelinganalogs.com/", qr'comics/.+\.png'],
        "Little Gamers"     => ["http://www.little-gamers.com/", qr'comics/\d{8}\.jpg'],
        "Frank and Ernest"  => ["http://www.comics.com/comics/franknernest/", qr'archive/images/frank.+(gif|jpg)'],
        "Theater Hopper"    => ["http://www.theaterhopper.com/", qr'vault/\d{6}\.jpg'],
        "The Born Loser"    => ["http://www.comics.com/comics/bornloser/", qr'archive.+bornloser\d+\.gif'],
        "Working Daze"      => ["http://www.comics.com/comics/workingdaze/", qr'archive/images/workingdaze\d+\.(jpg|gif)'],
    );
     
    my $xml_headers = <<'XML';
    <rss version="2.0">
      <channel>
        Comics
        http://gnuvince.net/~vince/comics
        <description>Comics</description>
        <language>en-us</language>
        <ttl>40</ttl>
    XML
     
    my $xml_footers = <<'XML';
      </channel>
    </rss>
    XML
     
    my $str_date = strftime("%a, %d %b %Y %T %Z", localtime);
     
     
     
     
    sub get_strip_url {
        my ($url, $regex, $base) = @_;
        if (!defined $base) {
            $base = $url;
        }
     
        my $src = get($url);
     
        if ($src) {
            my ($file_location) = $src =~ m{($regex)};
            return $base . $file_location if $file_location;
        }
        return "Couldn't fetch strip!";
    }
     
     
    sub print_item {
        my ($name, $link) = @_;
        print <<XML;
        <item>
          $name
          <category>$name</category>
          <description><img src="$link"></description>
          $link
          $str_date
          <guid>$link</guid>
        </item>
    XML
    }
     
     
    sub main {
        # Print the RSS feed
        print $xml_headers;
     
        while (my ($name, $params_ref) = each %strip)
        {
            my $t1 = threads->create( sub  { print_item($name, get_strip_url(@$params_ref)); } );
               $t1->detach();
        }
     
       print $xml_footers;
    }
     
    main() if !caller;
     
    1;
     
     
    __END__
     
    =head1 NAME
     
    comics
     
    =head1 DESCRIPTION
     
    comics is a simple script that generates an RSS feed with the daily
    strip of several web comics.  comics depends on LWP::Simple which
    can be installed from CPAN to fetch the data necessary to the
    construction of the feed.
     
    =head1 TODO
     
    =over
     
    =item *
     
    Add a configuration file into which people can put the strips they want
     
    =item *
     
    Get more strips in
     
    =back
     
    =head1 AUTHOR
     
    Vincent Foley-Bourgon, <vfoleybourgon at yahoo dot ca>
     
    =head1 COPYRIGHT AND LICENSE
     
    Copyright 2006 by Vincent Foley-Bourgon
     
    This library is free software; you can redistribute it and/or modify
    it under the same terms as Perl itself.
     
    =cut
    Vista powaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ::

Discussions similaires

  1. Realisation d'une pause avec les thread
    Par toitoine01 dans le forum Concurrence et multi-thread
    Réponses: 9
    Dernier message: 10/05/2006, 08h51
  2. [Avis aux pros !] Problèmes de la VCL avec les threads
    Par benj63 dans le forum C++Builder
    Réponses: 3
    Dernier message: 17/02/2006, 22h38
  3. Probleme avec les threads
    Par Orahn dans le forum MFC
    Réponses: 5
    Dernier message: 04/11/2005, 10h14
  4. Réponses: 5
    Dernier message: 10/05/2005, 10h22
  5. [langage] Perl a t'il été compiler avec les threads
    Par vodevil dans le forum Langage
    Réponses: 2
    Dernier message: 07/05/2005, 15h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo