cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
878
Views
0
Helpful
5
Replies

[ISSUE} YDK-PY CRUD service with Python flask microframework

rkotwani@cisco.com
Cisco Employee
Cisco Employee

Hello Experts,

 

I'd like some guidance on a script that gets operational data from xr router, then certain key/values are extracted from the json output, with the goal to display it on a webpage (using YDK-PY with FLASK microframework to display data on to the webpage.

 

when I call the flask app.router(/interfaceStats) decorater, my script seems to hang at the CRUD operation.  During the crud -

intPropObj1=crud.read(provider, intPropObj)

the script hangs, and so does the flask web server.

 

Regards,

RK

 

Code Snippet -

 

@app.route('/interfaceStats')

def interfaceStats():

  _init_logging()

 

  dir_path = os.path.dirname(os.path.realpath(__file__))

 

  private = "%s/id_rsa" % dir_path

  public = "%s/id_rsa.pub" % dir_path

  print(dir_path, private, public)

 

 

  provider = NetconfServiceProvider(address='192.168.253.144',

                                    username='rkotwani',

                                    protocol='ssh',

                                    timeout=0,

                                    port=22,

                                    private_key_path=private,

                                    public_key_path=public)

 

  codec = CodecService()

  provider1 = CodecServiceProvider(type="json")

  print("past codec provider1")

  crud = CRUDService()

  print("past crud instantiation")

  intPropObj=ifmgr_oper.InterfaceProperties()

  print("here")

# Getting stuck below.

  intPropObj1=crud.read(provider, intPropObj)

# Getting stuck at the above line

  print("here 2")

  jsonIntPropObj = codec.encode(provider1, intPropObj1)

  print(jsonIntPropObj)

  return render_template('index.html')

5 Replies 5

saalvare
Cisco Employee
Cisco Employee

were you able to sort out the problem?

ElvisLou15831
Level 1
Level 1

Hello,

 

Does anybody have an idea about this? we have the totally same issue with this.

 

The crudService.read ran well when we use single python script, but got stuck when it in the flask framework which as a http service.

 

please help me, thanks very much.

Hello Elvis Lou

In my opinion you are facing known and documented issue, which is related to multithreading. I believe the flask operator, spawns new thread, which interrupts thread with YDK related process. As a workaround you have to refactor structure of your script in a way that WHOLE YDK related operation is inclosed in one thread from start to finish.

Yan Gorelik
YDK Solutions

Thanks for the reply, yangorelik,

 

so it is not possible that run ydk api call concurrently in one process? 

is it must ran Serializable for any operation to cisco router?

You can have multiple threads in one process, but when it comes to execution of YDK involved operation, the last must be located in single thread.

The issue #825  has nothing to do with serialization, it comes from YDK design/implementation. Keep in mind that all actual operations on data is happening in C++ code, which is connected to Python code via Pybind11 adapter. When Python code sends request to C++ code the adapter locks the python thread in order to get reliably the response.

In your script example you import YDK related modules and initiate logger in main thread. The Flask call creates another thread to execute interfaceStats() function. That action confuses Pybind11 adapter and eventually causes the YDK thread to get stalled.

Yan Gorelik
YDK Solutions
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: