Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The first thread would run fine. But the second thread would always cause a crash

I've tried running your code as-is and cannot get it to crash (over the space of a few runs) on my perl which is This is perl 5, version 20, subversion 3 (v5.20.3) built for x86_64-linux-thread-multi. To remove some possible sources of error I then made a few changes and come up with this, which I can't get to crash either.

#!/usr/bin/perl -l use strict; use warnings FATAL => 'all'; use threads; use threads::shared; use HTTP::Headers; use LWP::UserAgent; my $counter_thread : shared; sub getgoog { my $method = "GET"; my $uri = "https://www.google.com/"; my $header = HTTP::Headers->new(); $header->push_header("Content-Type" => "application/json"); my $ua = LWP::UserAgent->new; $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); my $request = HTTP::Request->new(); $request->method($method); $request->uri($uri); $request->headers($header); my $response = $ua->request($request); print $counter_thread++, "th :", "Dumped Data Returned:\n\n ", sub +str ($response->decoded_content, 0, 256); print "Response code: ", $response->code, "\tHTTP Response Messag +e: ", $response->message, " "; print "Thread id: ", threads->tid(), "\n"; } my $thra = threads->create (\&getgoog); $thra-> join; my $thrb = threads->create (\&getgoog); $thrb-> join;

For clarity, the changes are:

  • De-duplicated the sub as both appear to be completely identical.
  • Remove the unused Threads::Queue
  • Removed use of Dumper (because it's acting on a scalar string)
  • Avoided use of the specials $a and $b
  • Truncated the output for visualisation reasons

What happens if you try running my code? You might also want to consider experimenting with not making warnings fatal.


In reply to Re: HTTP Request and Threads by hippo
in thread HTTP Request and Threads by banhmi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-24 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found