find if python connection is open or closed

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

amrsherif
Posts: 99
Joined: Mon Jul 23, 2018 4:34 pm

find if python connection is open or closed

Postby amrsherif » Tue Feb 08, 2022 8:49 am

Hello,

Is there a way to find out, in a program, if a python conneciton is open?

thanks

EViews Steve
EViews Developer
Posts: 788
Joined: Tue Sep 16, 2008 3:00 pm
Location: Irvine, CA

Re: find if python connection is open or closed

Postby EViews Steve » Tue Feb 08, 2022 12:47 pm

Sorry, not really.

If you call XGET while disconnected, you'll get an error, which you could test for.
If you call XCLOSE while already disconnected, no error is thrown.
If you call XOPEN while already connected, it closes the first XOPEN (if any), then re-opens to the new type (or same type).

Since there are multiple external types (python, matlab, r), you can't really tell what you're connected to if you're in a generalized subroutine. But since your XGET strings are really dependent on the external type, I'm not sure what a generalized subroutine would do for you. I could see more use cases for the XPUT side, but in that case you really shouldn't care what the external type is, just that there's a connection. And maybe not even that as your subroutine should only be called after already calling XOPEN. Or you could call XOPEN yourself within the routine based on passed in parameters.

I could be convinced for the need of a new "@XTYPE" method that would return a string denoting external connection type or empty string for no connection.

How are you planning on using this information? In what context?

Steve

amrsherif
Posts: 99
Joined: Mon Jul 23, 2018 4:34 pm

Re: find if python connection is open or closed

Postby amrsherif » Tue Feb 08, 2022 3:09 pm

Thanks for the detailed response.
I certainly considered trying to test for errors. It is not my prefered solution but I am willing to go with it if there is no other choice.

I am writing an addin, which calls python and will be used frequently. The process is already on the slow side, and opening and closing an external connection every single time makes things consierably slower. So my idea was to find out if one is already open, and if so, then skip the xopen(p) line/command.

Something like:

if @XTYPE=0 then
xopen(p)
move on to the rest of the code.

This would save me a couple of seconds every time i use the addin, and these seconds could add up.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: find if python connection is open or closed

Postby EViews Gareth » Tue Feb 08, 2022 3:19 pm

It might be cleaner to always just XClose at the start and then open your own connection.

If you're using a connection the user has opened prior to using your add-in, who knows what state that Python instance is in. They might have done some crazy stuff you're not expecting.

If you always open up your own connection, you're guaranteed it is clean. Well as clean as Python ever is.
Follow us on Twitter @IHSEViews

EViews Steve
EViews Developer
Posts: 788
Joined: Tue Sep 16, 2008 3:00 pm
Location: Irvine, CA

Re: find if python connection is open or closed

Postby EViews Steve » Wed Feb 09, 2022 10:44 am

Ok, I made two enhancements to EViews 12 to help with this...

First, XOPEN now has a "keepcurrent" option which will prevent it from closing and then reopening the connection if EViews is already connected to the same type. Use it like this:

Code: Select all

XOPEN(r, keepcurrent)


Second, I added a new @XTYPE method to return a string of any current connection's type. A connection to R returns "rconn", Matlab returns "m", and Python returns "pyconn". If there is no connection, it returns an empty string without an error. Use it like this:

Code: Select all

%t = @xtype


These two changes will be available in the next EViews 12 patch. Your addin will have to check for EViews version (@VERNUM) with a build date (@BUILDDATE) that is after today's to verify that the user has the proper version.

Here are the other @X methods that already exist, but these throw an error when there is no current connection:
@XGETNUM
@XGETSTR
@XVERSTR
@XVERNUM

amrsherif
Posts: 99
Joined: Mon Jul 23, 2018 4:34 pm

Re: find if python connection is open or closed

Postby amrsherif » Wed Feb 09, 2022 11:53 am

Thank you, I appreciate this. This is a much cleaner solution for me.

I am not sure if i posted this or not (I thought i did), but the users who will be using this do not know how to use python and are very light EViews users, so the only interaction they will have with python is through the addin. This helps speed things up for me.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13294
Joined: Tue Sep 16, 2008 5:38 pm

Re: find if python connection is open or closed

Postby EViews Gareth » Wed Feb 09, 2022 2:00 pm

Hopefully you'll have already installed python for them then ;) (Having spent a few hours trying to get it working on my new machine recently...)
Follow us on Twitter @IHSEViews

amrsherif
Posts: 99
Joined: Mon Jul 23, 2018 4:34 pm

Re: find if python connection is open or closed

Postby amrsherif » Wed Feb 09, 2022 3:50 pm

That's the next step. Not the most exciting part of the project and I am certaintly not looking forward to it, especially with admins rights and privelages : )


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 19 guests