PHP Thread?

Posted on 2004-09-23
Last Modified: 2012-06-27
I am performin an SNMP get a couple hundred times and right now it takes about 12 seconds to come back/connection. I was hoping to have several SNMP connections go out at once. Any help?
Question by:maxb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 35

Expert Comment

ID: 12137123
Create a list of targets for the SNMP get, then each time the SNMP procedure runs, read the top name off the list, and delete it from the list. Then, just run the script multiple times.

- J

Author Comment

ID: 12137143
Are you saying PHP does not support threads?
LVL 35

Expert Comment

ID: 12137449
Hmmm, not in the sense that you are thinking of, probably.

If you wanted, you could use sockets and turn off blocking mode so that the script continues to process while the data is being sent/returned, but I imagine that could be some rather complex and confusing scripting, depending on what your script does. That might not even work, since you're probably using the SNMP extension instead of raw sockets, and turning off blocking probably wouldn't affect the sockets that SNMP uses.

However, since most web servers are capable of threading, the much easier solution would be to run the script multiple times.

Here's an article on non-blocking/blocking sockets:

In any event, there's one more thing you could do. If you're familiar with how to talk to a device's SNMP using sockets instead of the SNMP library, then you could do something like this:

// Create Socket 1 to SNMP port on device
// Create Socket 2 to SNMP port on device
// Create Socket 3 to SNMP port on device

// Set non-blocking mode on Socket 1
// Set non-blocking mode on Socket 2
// Set non-blocking mode on Socket 3

// Query SNMP on Socket 1
// Query SNMP on Socket 2
// Query SNMP on Socket 3

// Process the results

// Close all sockets

// If you have more devices, repeat with t he next set.

- Jonathan
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12193676
Sounds like there is no simple solution. Hmm...
LVL 35

Expert Comment

ID: 12210775
Is there a problem with running multiple instances of the script? You can easily set up something that calls the script multiple times automatically and repeats until finished - completely in PHP, too.

Here's a combined way of doing it:

Create a simple script that accepts the device's SNMP name via query string, like:

Then, have getsnmp.php use the standard SNMP library to do the dirty work. Now create a master SNMP running script that uses non-blocking sockets to call the script several times. That master script can query a database, and loop through a list of device names, running 5 or so instances of getsnmp.php at a time, passing the device names through the URL. You only need to open the socket in non-blocking mode, and send the HTTP commands to fetch the page, which is easy to do. Just open a socket to the host on port 80 and send the following 2 lines of text, each line followed by a carriage return (\r\n):

GET /path/to/getsnmp.php?device=devicenamehere HTTP/1.0
<then one more carriage return \r\n for a blank line>

That will start a HTTP request to the server, which will begin running the getsnmp.php script, and you can fread() the results. At that point, you're basically allowing the web server to perform the threading, so you get the benefit of multi-threading and no difference in actual results.

But like I said, if there's a problem with this solution, let us know. Threading isn't really a simple issue to begin with, so this is relatively pretty simple.

- J
LVL 35

Expert Comment

ID: 12281616
The author hasn't provided any reason as to why my solution would not work for him. If it doesn't work, please let us know why, so others can benefit from reading this later.

Threading isn't an easy issue in any language, so the answer will be tough. That's no reason to try to back out of it or ignore the answer.

- J

Author Comment

ID: 12281887
The solution required running a single instance of a script with several threads calling the snmpget command. I ended up going with Perl instead of PHP because I did not wish to make my own direct SNMP calls and Perl supports multithreading and using the SNMP library.
LVL 35

Expert Comment

ID: 12283028
Okay, good to know that it worked out. Thanks!

- J

Accepted Solution

ee_ai_construct earned 0 total points
ID: 12321384
Question answered by asker or dialog valuable.
Closed, 125 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Correct syntax for subdirectories 9 33
Ahax pagination 9 47
Displaying a google org chart from php/mysql code 26 14
php hashing methods 3 10
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question