0

Trying to make a small example on how to do IPC with Elisp using Python for the server. My source of information is this Here is the code I have right now.

my-process.el

(let ((process-connection-type nil)
      (arguments (list "./server.py")))
  (apply 'start-process "my-process" "*my-process-buffer*" "python" arguments)
  (set-process-filter (get-process "my-process") 'my-callback))

(defun my-callback (process msg)
  (message msg))

(defun kill-my-process ()
  (interactive)
  (delete-process "my-process"))

(defun send-to-my-process (msg)
  (interactive "sEnter message: ")
  (process-send-string "my-process" (concat msg "\n")))

(provide my-process)

server.py

import sys

sys.stdout.write("STARTING MY PROCESS!")
sys.stdout.flush()
for line in sys.stdin:
    sys.stdout.write(line)
    sys.stdout.flush()

The problem I have is that the process starts ok, but I can only see "STARTING MY PROCESS!" printed nothing when I do send-to-my-process.

My question is, what do I need to change in order for messaging to work in the above example?

Update: Added suggested fixes.

1

On the elisp side, you should send a final newline to the client:

(process-send-string "my-process" (concat msg "\n")))

On the python side, you need to flush the output since you're using a pipe:

sys.stdout.flush()
  • That helped some, now "STARTING MY PROCESS!" prints in the messages buffer but still when I do send-to-my-process nothing happens. – user3139545 May 6 '16 at 16:45
  • 1
    It works for me using Python 3. It doesn't work using Python 2, and I'm too lazy to work out why. – jch May 8 '16 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.