httpClient() question

I'm working on communicating data to a Diagraph IJ4000 packaging line printer. Right now I've settled on using system.net.httpClient() to send the data. The scripting is very basic, as the printer has built-in cgi scripts where all I'm doing is sending a GET and the URL that includes the target cgi and any data parameters.

My Project script looks as such:

# Create the JythonHttpClient.
client = system.net.httpClient()

def loadDiagraph(ipAddress,printTemplate,lotNumber,mfgDate,expireDate):
		#Send Data & Start Print requst 
		client.get("http://"+ipAddress+"/serial.cgi?idx=0&nme="+printTemplate+"&fst=1&lst=3&d1="+lotNumber+"&d2="+mfgDate+"&d3="+expireDate+"&net=1")

I'm then calling the script from inside a "Load Printer" button I've placed on a screen.
This works without fault, but I've read through multiple forum threads about being careful with calling the system.net.httpClient() too much or in specific scenarios - most of which is beyond my current knowledge and I'm not understanding.

Right now we're testing a single printer, but I could end up with as many as 6-7 line printers which I'll need to send data to in this manner.

Is there a way to see if I've opened too many connections? Do I need to close the connection(s) somehow after sending the data? If so, how?

Thank you for any assistance!

You're fine. You created the client variable outside any function definition, making it shared across all callers. Which is precisely what you should do.

3 Likes

The concern is more something like "creating an instance of httpClient every single time a particular button is pressed in a Perspective project" - which means you're creating an instance per session multiplied by the number of times end users click the button - very quickly getting out of control and potentially exhausting system memory and other OS level limitations as those clients hold on to additional memory and thread pools.

It's such a concern that in 8.3 there's going to be a 'default' HTTP client instance mounted as system.net.httpClient - you'll still be able to construct your own, but for simple cases that don't need customization like your example above, you'll be able to just use system.net.httpClient.get(url).

5 Likes

Thanks for the insight!