Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


PHP Thread?

Posted on 2004-09-23
Medium Priority
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
Independent Software Vendors: 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
Host: www.domain.com
<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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

730 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