Home > Community > Forums > Custom IC SKILL > IPC communication between Python and Cadence

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 IPC communication between Python and Cadence 

Last post Wed, Aug 17 2011 2:19 PM by Vijay Srini. 4 replies.
Started by Vijay Srini 16 Aug 2011 10:27 PM. Topic has 4 replies and 3714 views
Page 1 of 1 (5 items)
Sort Posts:
  • Tue, Aug 16 2011 10:27 PM

    IPC communication between Python and Cadence Reply

    Hi All,

     I am trying to open a IPC channel between python and cadence. I managed to get one way communication from either to the other, but unable to get two way communication.

     Please help to point me in the right direction!

     

     Thanks in advance,

     

    Vijay

    Filed under: , , , , ,
    • Post Points: 20
  • Wed, Aug 17 2011 6:53 AM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: IPC communication between Python and Cadence Reply

    I've done it this way:

    Skill:
    pyIpc = ipcBeginProcess("my.py " "" 'myIpcDataHandler 'myIpcErrHandler 'myIpcFinishHandler "/tmp/py.log")
    procedure(myIpcDataHandler(ipcId data)
      let((result)
        ;printf("in myIpcDataHandler with %L\n" data)
        foreach(d parseString(data "\n")
            unless(errset(result=evalstring(d))
                 printf("ERROR: %L\n" errset.errset)
                 ipcWriteProcess(ipcId sprintf(nil "ERROR %L\n" errset.errset))
            ) ; unless
        )
      )
    )

    procedure(myIpcErrHandler(ipcId data)
        println(list("ERROR: myIpcErrHandler called with:" ipcId data))
    )

    procedure(myIpcFinishHandler(ipcId data)
      let((exitStatus)
        printf("Finished at %s: %L %L  " getCurrentTime() ipcId data)
        exitStatus = ipcGetExitStatus(ipcId)
        printf("Return value = %L\n" exitStatus)
        if(exitStatus==0 then
            printf("The IPC call has finished!!\n")
        else
            printf("**ERROR: operation failed\n")
        )
      )
    )

    procedure(mySkillCommand(data)
        println(data)
        ipcWriteProcess(pyIpc "Skill says hello")
    )

    Python file my.py:
    import sys
    data = 'Hello from the Python world'
    sys.stdout.write('mySkillCommand("%s")\n' % data)
    sys.stdout.flush()
    returnData = sys.stdin.readline()
    #Write to a file since stdout goes to Cadence
    outFile = open('/tmp/myPy.out','w')   
    outFile.writelines( returnData )
    outFile.close()

    When done, /tmp/py.log shows commands sent from Python to skill through stdout and /tmp/myPy.out shows the Skill response.

    Derek

    • Post Points: 20
  • Wed, Aug 17 2011 11:08 AM

    Re: IPC communication between Python and Cadence Reply

    Hi Derek,

     

    Thank you for your quick help. Everything goes fine but I am not able to read the returnData. The log file has only the "mySkillCommand..."

    I seem to have some issue with read timing.

    I tried doing it another way like the following:

    Python:

    import sys
    import os
    sys.stdout.flush()

    os.write(3,"(println \"Hello World!!\")")
    os.write(3,"(println \"Second time, Hello World!!\")")
    os.write(3,"(3+3)")

    a="(nil)"

    while "(nil)" in a:

        a="Start:",os.read(4,4096)

     

    SKILL:

    cid = ipcSkillProcess("python ~/scripts/skill/ipc_py/test2.py")

     

     I still have some timing issue, which improved with that while loop. Cadence seemed to return "(nil)" each time the read timing missed. Is there anyway to permanently resolve this issue?

     

     

    Thanks,

    Vijay

    • Post Points: 20
  • Wed, Aug 17 2011 12:06 PM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: IPC communication between Python and Cadence Reply

    I had trouble with ipcSkillProcess and the 3&4 streams. That is why I chose to use ipcBeginProcess and used stdin and stdout instead. Also, in the Python, I limited my code to one readLine and had my skill code only send a string that contained one \n as the terminating character. This helped me with my communication problems. What I found was that if I sent multiple carriage returns, I needed a readline for each one. If I only performed one readline, then the next one was queued up for next time. If I used a while or a readLines, the Python would hang waiting on the stdin to finish. Maybe you can send it an EOF type character to get the Python to continue. I just coded around it so that Cadence always sent only one line back to Python for every command Python sent to Cadence. This was sufficient for me.

    Derek

    • Post Points: 20
  • Wed, Aug 17 2011 2:19 PM

    Re: IPC communication between Python and Cadence Reply

    Hi Derek,

     

    Thanks for the explanation! Caught the reason why your way failed for me. I had terminated the return with a "\n". So the readline was failing. I added the newline character and now it seems to be working fine.

     

    Thanks a lot for your help!

     

    Thanks once again,

     

    Vijay

    • Post Points: 5
Page 1 of 1 (5 items)
Sort Posts:
Started by Vijay Srini at 16 Aug 2011 10:27 PM. Topic has 4 replies.