Are you over 18 and want to see adult content?
1
More Annotations
A complete backup of livinginindonesiaforum.org
livinginindonesiaforum.org
Are you over 18 and want to see adult content?
A complete backup of vacalocamultshow.com.br
vacalocamultshow.com.br
Are you over 18 and want to see adult content?
1
A complete backup of atlanticoaks.com
atlanticoaks.com
Are you over 18 and want to see adult content?
2
Favourite Annotations
A complete backup of dudleynews.co.uk
dudleynews.co.uk
Are you over 18 and want to see adult content?
2
A complete backup of kantaribopemedia.com
kantaribopemedia.com
Are you over 18 and want to see adult content?
2
Text
* Alan Storm
* the professional weblog; because we have to* About
* Archive__
* Programming Quickies* PHP Internals
* Magento
* Magento 2
* Modern Javascript
* Laravel
* Modern PHP
* OroCRM
* AppleScript
* SugarCRM
* Python
* Drupal
* Contact
* __
* __
__ __ __
* About
* Archive
* Programming Quickies* PHP Internals
* Magento
* Magento 2
* Modern Javascript
* Laravel
* Modern PHP
* OroCRM
* AppleScript
* SugarCRM
* Python
* Drupal
* Contact
CATEGORIES
-------------------------* AppleScript
* Drupal
* Laravel
* Magento
* Magento 2
* Modern Javascript
* Modern PHP
* OroCRM
* PHP Internals
* Programming Quickies* Python
* SugarCRM
* Uncategorized
* WebOS
RECENT POSTS
------------------------- * The End of 10-Digit ISBNs * Pestle Windows Support * Reporting Bugs Even when you Know they’ll be Closed * Commerce Bug 3.2.4 Released! * Async PHP: Symfony’s HttpClient 4.3ARCHIVES
-------------------------* March 2020
* February 2020
* January 2020
* December 2019
* November 2019
* October 2019
* August 2019
* July 2019
* June 2019
* May 2019
* April 2019
* March 2019
* February 2019
* January 2019
* December 2018
* November 2018
* October 2018
* September 2018
* August 2018
* July 2018
* June 2018
* May 2018
* April 2018
* March 2018
* February 2018
* January 2018
* December 2017
* November 2017
* October 2017
* September 2017
* August 2017
* July 2017
* June 2017
* May 2017
* April 2017
* March 2017
* February 2017
* January 2017
* December 2016
* November 2016
* October 2016
* September 2016
* August 2016
* July 2016
* June 2016
* May 2016
* April 2016
* March 2016
* February 2016
* January 2016
* December 2015
* November 2015
* October 2015
* September 2015
* August 2015
* July 2015
* June 2015
* May 2015
* March 2015
* February 2015
* January 2015
* December 2014
* November 2014
* October 2014
* September 2014
* July 2014
* June 2014
* May 2014
* April 2014
* March 2014
* February 2014
* January 2014
* December 2013
* November 2013
* October 2013
* September 2013
* August 2013
* July 2013
* June 2013
* May 2013
* April 2013
* March 2013
* February 2013
* January 2013
* December 2012
* November 2012
* October 2012
* September 2012
* August 2012
* July 2012
* June 2012
* May 2012
* April 2012
* March 2012
* February 2012
* January 2012
* December 2011
* November 2011
* October 2011
* September 2011
* August 2011
* July 2011
* June 2011
* May 2011
* April 2011
* March 2011
* February 2011
* January 2011
* December 2010
* November 2010
* October 2010
* September 2010
* August 2010
* July 2010
* June 2010
* May 2010
* April 2010
* March 2010
* February 2010
* December 2009
* November 2009
* October 2009
* September 2009
* August 2009
* July 2009
* June 2009
* May 2009
* April 2009
* March 2009
* January 2009
* December 2008
* November 2008
* October 2008
* September 2008
* August 2008
* June 2008
* May 2008
* April 2008
* March 2008
* January 2008
* December 2007
* November 2007
* October 2007
* August 2007
* July 2007
* June 2007
* May 2007
* March 2007
* January 2007
* November 2006
* October 2006
* September 2006
* August 2006
* July 2006
* June 2006
* May 2006
* April 2006
* March 2006
* February 2006
* January 2006
* November 2005
* September 2005
* August 2005
* July 2005
* June 2005
* May 2005
* April 2005
* March 2005
* February 2005
* January 2005
* December 2004
* November 2004
* October 2004
* September 2004
* August 2004
* July 2004
* June 2004
* May 2004
Copyright © Alan Storm 1975 – 2020 All Rights Reserved* Modern PHP
* 13 Min Read
* 13th February 2020 ASYNC PHP: SYMFONY’S HTTPCLIENT 4.3astorm
Frustrated by Magento? Then you’ll love Commerce Bug, the must have
debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copyand start working
WITH Magento instead of against it. UPDATED FOR MAGENTO 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today! PROGRAMMING QUICKIES Quick dispatches from the life of a working programmer.*
The End of 10-Digit ISBNs*
Pestle Windows Support*
Reporting Bugs Even when you Know they'll be Closed*
Commerce Bug 3.2.4 Released!*
Changing WordPress Host Names This entry is part 1 of 1 in the series Async PHP . This is the first post inthe series.
One luxury afforded to the average PHP programmer is never having to worry about threads or asynchronous programming. Unlike other dynamic languages of its era (ruby, python, etc.), PHP has no built-in concept of a thread. PHP also came along early enough that “asynchronous programming” wasn’t really a thing, and the language wasn’t built with any asynchronous primitives. Those that were added later (like generators) haven’t seen widespread adoption. For most PHP use cases “async” programming has meant sending a message to a job queue and running work offline. This is, however, starting to change. Programming environments like NodeJS are making asynchronous programming techniques increasingly mainstream. This mainstream exposure has pushed other programming environments to explore async concepts. PHP is no exception. In this new series we’ll explore how async concepts are leaking into PHP from the edges, explain how these APIs work, and describe the challenges involved in programming asynchronously in an environment that’s not designed for it. Today we’ll start small and discuss the Symfony projects’sHttpClient library
and its
asynchronous features. THREADS AND ASYNCHRONOUS PROGRAMMING Before we start, let’s talk about what we mean when we say asynchronous programming. In its most primitive form, a program is a simple series of steps.* Go to the store
* Get loaf of bread
* Get bottle of milk * Get stick of butter* Pay for groceries
* Go home
Most operating systems, (and the programming languages used inside of them), have the concept of a thread . In over simplified terms, a thread lets you run two sets of code inside a single computer program at once. Another way to describe this is running code _inparallel_.
If we were to describe threads in terms of the above steps, a thread might be an adult (the main thread) sending three of their kids (the child threads) to the store to get the three items — or it might be a parent going to the store with three children and sending each one to a different aisle for the three items. Threads help programmers access the full processing power of their computer, but threaded programming is hard to get right. Two threads of execution are not guaranteed to execute identically every time. Once a thread is started there’s no _direct_ way to control it — it’s up to the end-user-programmer to coordinate between threads through shared global state. One technique for this is called Mutexlocking
).
The unpredictability of threads creates the sort of unpredictable, hard to reproduce bugs and behavior that are the bane of any softwareproject.
Newer languages and systems have tried to improve on threads or force end-user-programmers down threaded paths that are less unpredictable. Two examples of this are go’s go routinesand channels
. Another is Apple’s grandcentral dispatch .
ASYNCHRONOUS PROGRAMMING VS. THREADS When we talk about asynchronous programming in this series, we’re talking about how _single programs_ can execute in an order other than the order the code’s written in _WITHOUT_ directly using threads. So while external job-queue systems could accurately be described as asynchronous programming, that’s not what we’re interested intoday.
SYNCHRONOUS HTTP REQUESTS Now that we’re constrained by a few definitions, let’s get started. We’re going to write a small PHP program that will* Output some text
* Fetch the https://httpstat.us/200?sleep=5000 URL * Output some more text At the time of this writing httpstat.us is a publicly available but privately run web-service that lets you * Make an HTTP request * Tell that request what status code it should return * And how long to sleep before returning it The URL https://httpstat.us/200?sleep=5000 will sleep for 5 seconds and then return a 200 OK status code. Let’s create the program to make that request#File: working.php
namespace Pulsestorm\Tutorials\AsyncPhp; function main($argv) { echo "About to make the request","\n"; echo "...","\n";
echo file_get_contents('https://httpstat.us/200?sleep=5000'); echo "Made the request!"; }
main($argv);
and then run it.
$ php working.php
About to make the request ...
// 5 second pause ... not actually printed Made the request!
Pay attention to the output of your program as it runs. You’ll notice that it paused for around 5 seconds after outputting .... That’s because your program was waiting for the HTTPS request to finish. You’ll get the same results if you try to use PHP’s curl_* functions
namespace Pulsestorm\Tutorials\AsyncPhp; function downloadPage($path){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$path); curl_setopt($ch, CURLOPT_FAILONERROR,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $retValue = curl_exec($ch); curl_close($ch);
return $retValue;
}
function main($argv) { echo "About to make the request","\n"; echo "...","\n";
$test = downloadPage('https://httpstat.us/200?sleep=5000'); echo "Made the request!"; }
main($argv);
Both the file_get_contents URL wrappers and the curl_* functions operate synchronously. Once you make an HTTP request your program needs to wait until it finishes before moving on to the next line. SYMFONY HTTPCLIENT
Now we’re going to compare the above to Symfony’s HttpClient library. We’ll use composer to install the latest 4.3.x version of this library
$ composer require symfony/http-client:~4.3.0 and then rewrite our small program to use Symfony’s HTTP client. #File: working.php
namespace Pulsestorm\Tutorials\AsyncPhp; use Symfony\Component\HttpClient\HttpClient; require 'vendor/autoload.php'; function main($argv, $httpClient) { echo "About to make the request","\n"; $response = $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); echo "Made the request!","\n"; echo "...","\n";
}
main($argv, HttpClient::create()); If we run this program, we’ll see the following output. $ php working.php
About to make the request Made the request!
...
// 5 second pause ... not actually printed This time our program printed out _About to make the request_ and _Made the request_ immediately. Unlike the curl and file_get_contents example, our call to HttpClient’s request method executed immediately and _appears_ to have finished making the request in the background. Our program still halted at the end to finish the request, but while our program was running it behaved a like an async program would.
How is this possible in a language without async or threading primitives?
PHP IS JUST C
Readers of the There’s no Such Thing as PHP article may know
where this is going. While PHP has no threads or (as of this writing) no async primitives, the PHP “runtime” is just a collection of compiled C code (called extensions). The distributed nature of how the PHP project is run means there isn’t even “one true PHP” — there’s just different vendors and package maintainers putting together a version of PHP. There’s nothing stopping a PHP extension developer from using a thread to build async or async-like behavior into their functions and classes. In fact, a number of extensions do this. The Symfony HttpClient authors have identified these hidden places where there’s async-like behavior in PHP and used it to build an HTTP client library that doesn’t block the main execution of your program. Tracking down _how_ the symfony HttpClient library manages this, and how the C code that implements this works, is beyond the scope of this article. If you’re the curious sort though the two places to get started are
* The Symfony\Component\HttpClient\CurlHttpClient class * The Symfony\Component\HttpClient\NativeHttpClient class The Symfony HttpClient library will, by default, attempt to use the Symfony\Component\HttpClient\CurlHttpClient to make your HTTP request. If the curl_* functions are not available in your particular installation of PHP, HttpClient will fall back to using the Symfony\Component\HttpClient\NativeHttpClient class. This “native” client uses PHP’s stream context functionality to
ensure the HTTP requests made are not synchronous. AWKWARD APIS
While this is an interesting use of PHP, it’s not without its downsides. Consider the following program, which is very similar to the one we ran above. namespace Pulsestorm\Tutorials\AsyncPhp; use Symfony\Component\HttpClient\HttpClient; require 'vendor/autoload.php'; function main($argv, $httpClient) { echo "About to make the request","\n"; $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); echo "Made the request!","\n"; echo "...","\n";
}
main($argv, HttpClient::create()); Run this program,
$ php working.php
About to make the request // 5 second pause ... not actually printed Made the request!
...
and you’ll see the 5 second pause happens between _About to make the request_ and _Made the request!_. This tells us our program stopped and waited for the HTTP request to finish. The problem is this line. $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); We never assigned the return value of $httpClient->request call to a variable. This means the PHP garbage collector thought it was OK to free the memory for the object that would normally be returned for this function, and Symfony’s HTTP client library made sure to finish the request before the object went away forever .
So the request finishes, but finishes synchronously. It’s an odd bug ,
and not the sort of thing you want to track down when all you’re trying to do is make some HTTP requests efficiently. It’s also inevitable when you’re trying to bring new concepts into a language that’s not designed for them. To really make use of this asynchronous behavior, PHP programmers need an extension that will give them first-class asynchronous primitives. Our next task for this series? Find one of those extensions and give it a try.
Modern PHP
Permalink: https://alanstorm.com/async-php-symfonys-httpclient-4-3/ Copyright © Alan Storm 1975 – 2020 All Rights Reserved Originally Posted: 13th February 2020 * Share:
* __
* __
* __
* __
Author
ASTORM
Copyright © Alan Storm 1975 – 2020 All Rights Reserved
curl_close($ch);
return $retValue;
}
function main($argv) { echo "About to make the request","\n";echo "...","\n";
$test = downloadPage('https://httpstat.us/200?sleep=5000'); echo "Made the request!";}
main($argv);
Both the file_get_contents URL wrappers and the curl_* functions operate synchronously. Once you make an HTTP request your program needs to wait until it finishes before moving on to the next line.SYMFONY HTTPCLIENT
Now we’re going to compare the above to Symfony’s HttpClient library. We’ll use composer to install the latest 4.3.x version ofthis library
$ composer require symfony/http-client:~4.3.0 and then rewrite our small program to use Symfony’s HTTP client.#File: working.php
namespace Pulsestorm\Tutorials\AsyncPhp; use Symfony\Component\HttpClient\HttpClient; require 'vendor/autoload.php'; function main($argv, $httpClient) { echo "About to make the request","\n"; $response = $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); echo "Made the request!","\n"; echo "...","\n";
}
main($argv, HttpClient::create()); If we run this program, we’ll see the following output. $ php working.php
About to make the request Made the request!
...
// 5 second pause ... not actually printed This time our program printed out _About to make the request_ and _Made the request_ immediately. Unlike the curl and file_get_contents example, our call to HttpClient’s request method executed immediately and _appears_ to have finished making the request in the background. Our program still halted at the end to finish the request, but while our program was running it behaved a like an async program would.
How is this possible in a language without async or threading primitives?
PHP IS JUST C
Readers of the There’s no Such Thing as PHP article may know
where this is going. While PHP has no threads or (as of this writing) no async primitives, the PHP “runtime” is just a collection of compiled C code (called extensions). The distributed nature of how the PHP project is run means there isn’t even “one true PHP” — there’s just different vendors and package maintainers putting together a version of PHP. There’s nothing stopping a PHP extension developer from using a thread to build async or async-like behavior into their functions and classes. In fact, a number of extensions do this. The Symfony HttpClient authors have identified these hidden places where there’s async-like behavior in PHP and used it to build an HTTP client library that doesn’t block the main execution of your program. Tracking down _how_ the symfony HttpClient library manages this, and how the C code that implements this works, is beyond the scope of this article. If you’re the curious sort though the two places to get started are
* The Symfony\Component\HttpClient\CurlHttpClient class * The Symfony\Component\HttpClient\NativeHttpClient class The Symfony HttpClient library will, by default, attempt to use the Symfony\Component\HttpClient\CurlHttpClient to make your HTTP request. If the curl_* functions are not available in your particular installation of PHP, HttpClient will fall back to using the Symfony\Component\HttpClient\NativeHttpClient class. This “native” client uses PHP’s stream context functionality to
ensure the HTTP requests made are not synchronous. AWKWARD APIS
While this is an interesting use of PHP, it’s not without its downsides. Consider the following program, which is very similar to the one we ran above. namespace Pulsestorm\Tutorials\AsyncPhp; use Symfony\Component\HttpClient\HttpClient; require 'vendor/autoload.php'; function main($argv, $httpClient) { echo "About to make the request","\n"; $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); echo "Made the request!","\n"; echo "...","\n";
}
main($argv, HttpClient::create()); Run this program,
$ php working.php
About to make the request // 5 second pause ... not actually printed Made the request!
...
and you’ll see the 5 second pause happens between _About to make the request_ and _Made the request!_. This tells us our program stopped and waited for the HTTP request to finish. The problem is this line. $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); We never assigned the return value of $httpClient->request call to a variable. This means the PHP garbage collector thought it was OK to free the memory for the object that would normally be returned for this function, and Symfony’s HTTP client library made sure to finish the request before the object went away forever .
So the request finishes, but finishes synchronously. It’s an odd bug ,
and not the sort of thing you want to track down when all you’re trying to do is make some HTTP requests efficiently. It’s also inevitable when you’re trying to bring new concepts into a language that’s not designed for them. To really make use of this asynchronous behavior, PHP programmers need an extension that will give them first-class asynchronous primitives. Our next task for this series? Find one of those extensions and give it a try.
Modern PHP
Permalink: https://alanstorm.com/async-php-symfonys-httpclient-4-3/ Copyright © Alan Storm 1975 – 2020 All Rights Reserved Originally Posted: 13th February 2020 * Share:
* __
* __
* __
* __
Author
ASTORM
Copyright © Alan Storm 1975 – 2020 All Rights Reserved
echo "...","\n";
}
main($argv, HttpClient::create());Run this program,
$ php working.php
About to make the request // 5 second pause ... not actually printedMade the request!
...
and you’ll see the 5 second pause happens between _About to make the request_ and _Made the request!_. This tells us our program stopped and waited for the HTTP request to finish. The problem is this line. $httpClient->request('GET', 'https://httpstat.us/200?sleep=5000'); We never assigned the return value of $httpClient->request call to a variable. This means the PHP garbage collector thought it was OK to free the memory for the object that would normally be returned for this function, and Symfony’s HTTP client library made sure to finish the request before the object went away forever.
So the request finishes, but finishes synchronously.It’s an odd bug ,
and not the sort of thing you want to track down when all you’re trying to do is make some HTTP requests efficiently. It’s also inevitable when you’re trying to bring new concepts into a language that’s not designed for them. To really make use of this asynchronous behavior, PHP programmers need an extension that will give them first-class asynchronous primitives. Our next task for this series? Find one of those extensions and giveit a try.
Modern PHP
Permalink: https://alanstorm.com/async-php-symfonys-httpclient-4-3/ Copyright © Alan Storm 1975 – 2020 All Rights Reserved Originally Posted: 13th February 2020* Share:
* __
* __
* __
* __
Author
ASTORM
Copyright © Alan Storm 1975 – 2020 All Rights ReservedDetails
3
Copyright © 2024 ArchiveBay.com. All rights reserved. Terms of Use | Privacy Policy | DMCA | 2021 | Feedback | Advertising | RSS 2.0