views:

46

answers:

2

I'm using curl_multi functions to request multiple URLs and process them as they complete. As one connection completes all I really have is the cURL handle (and associated data) from curl_multi_info_read().

The URLs come from a job queue, and once processed I need to remove the job from the queue. I don't want to rely on the URL to identify the job (there shouldn't be duplicate URLs, but what if there is).

The solution I've worked up so far is to use the cURL handle as an array key pointing to the jobid. Form what I can tell, when treated as a string the handle is something like:

"Resource id #1"

That seams reasonably unique to me. The basic code is:

$ch = curl_init($job->getUrl());
$handles[$ch] = $job;
//then later
$done = curl_multi_info_read($master);
$handles[$done['handle']]->delete();
curl_multi_remove_handle($master, $done['handle']);

Is the cURL handle safe to use in this way?

Or is there a better way to map the cURL handles to the job that created them?

+2  A: 

It will probably work thanks to some implicit type cast, but it doesn't feel right to me at all. I think it's begging for trouble somewhere down the line, with future versions that treat resources differently, different platforms...

I personally wouldn't do it, but use numeric indexes.

Pekka
But with numeric indexes I'd still have to somehow map the original job to the cURL handle. You think array_search($ch, $jobs) would work?
Tim Lytle
@Tim I don't know, you'd have to try... True, you'd have to map the original job somehow with a numeric index, but IMO it's worth the effort. `array[$ch]` smells really evil!
Pekka
+1  A: 

I have to agree with Pekka... it will probably work but it smells bad. id use straight up integers as Pekka suggests or wrap the handles in a simple class and then use spl_object_hash or have the constructor generate a uniqid when its set up.

prodigitalson
Not sure wrapping the handles in a class helps, because all I get back from curl_multi_info_read() is the [unwrapped] handle.
Tim Lytle
Yeah i guess youd have to make a wrapper for the curl_multi functionality as well.
prodigitalson