1

I'm building an Objective-C mac app. I'd like that app to be able to start and continuously communicate with the same running python process.

For example, the basic flow might be:

  1. App starts, I start a python process in the background
  2. The app imports some libraries and initializes some variables in the python process
  3. User does something, I run some python code on this python process using the initialized variables and use the result of the python expression in my mac app

What techniques can I use to accomplish this? Any guidance would be incredibly helpful.

oshea
  • 186
  • 1
  • 6

2 Answers2

2

A possible solution is to run a mock web service by your Python process. Define your own interfaces (most likely RESTful APIs) for your Objective-C app to access. Maybe it will be a little expensive in performance - it depends on the complexity of your actual task and the amount of data you want to transfer between the two processes.

For example, in Python process, run a standard HTTP service on 8080 port, listening all the time. Then the Obj-C app send a request to localhost, something like:

http://127.0.0.1:8080/my_service/start_task
http://127.0.0.1:8080/my_service/get_progress
http://127.0.0.1:8080/my_service/get_result

and Python handles that request, do something and return the result in HTTP response.

By the way, maybe you could consider calling Python methods directly by C interface in your Obj-C app rather than run Python scripts in a seperate process.

Hailei
  • 42,163
  • 6
  • 44
  • 69
  • This sounds like a decent approach and may end up being the way I go, but I'm hoping for an easier way to communicate between the running processes. I will accept this answer if there aren't any better suggestions made. – oshea Apr 26 '12 at 05:58
0

In my eyes the simplest way to establish communication between two applications is the client-server protocol XMLRPC. Both Cocoa and Python do support it.

The Python part is fairly simple:

import xmlrpc.client
rpcProxy = xmlrpc.client.ServerProxy(URL_OF_SERVER)
rpcProxy.doJobInMacApp(arg1, arg2)

As for the Objective-C-part, I don't know, but it seems to be possible: XML-RPC Server in Cocoa or Best way to use XML-RPC in Cocoa application?

Community
  • 1
  • 1
gecco
  • 17,969
  • 11
  • 51
  • 68