1

I'm new to Python and I want my Python script to be able to communicate with my Windows program (developed in Delphi, FWIW).

Basically, the Python script will download a set of data from other data sources, and for each record it's downloaded, I'll log it and tell the Windows program. For logging I'll use the Python standard logging library if possible, but what's the easiest way to tell the win32 program so that I can show the process of the downloading to the end user?

Note: I know Python4Delphi, but it's not documented well, and I want to keep things simple.

Edit 1: There will be only one Delphi exe and multiple python scripts.

Thanks.

2
  • How is the Delphi program going to get the data? Is it going to read a file? Commented Mar 31, 2011 at 15:55
  • Hi David, what you are asking is what I'm asking ;) Commented Mar 31, 2011 at 16:51

5 Answers 5

3

If your Delphi program is the one executing the Python program, then you can simply have the script write progress messages to standard output, and you can read them in your Delphi program. (If you do it this way, then it doesn't matter that one program is in Python and the other is in Delphi. Either program can be written in whatever language you want.)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Rob, but I'm wondering if we can call CreateProcess in a thread to run the python script? Since there will be only one Delphi exe and multiple python scripts.
Of course. It doesn't really matter since CreateProcess creates a new process, completely unrelated to whatever thread called it. What you're probably concerned about is the code that runs afterward to receive the scripts' output. You can run that in threads, or you can process them all from one. (I've just noticed that the answer in the question I linked to does it wrong; it waits for the whole process to terminate before reading any of its output, which is wrong for multiple reasons. If you go this route, post a new question: How do I read output from multiple spawned processes?)
+1 for the suggestion, IMHO, this is the best because the independence gained at both sides ;)
It's actually severely limited. Your program must output everything and then have that output read, all at once. You can't use it interactively, and as your output tends towards something other than flat text, and only a small amount of it, this gets less and less pretty.
@Rob, why I'm asking creating new python process in threads because I want to use each thread to run and capture output from python, then send it to the main UI to display to the user, as Warren P said, I'll have to communicate with python interactively.
|
1

You could use a named pipe or a socket to communicate between the Python code and the Delphi code. For interfacing Python to named pipes you could could use ctypes (example here).

Altermatively, you could create a COM component in Delphi and make calls to it from Python (ActivePython includes all the Windows bits you need).

1 Comment

Thanks Vinay, COM will be too completed. Named pipes is a good idea!
1

Use PyWin32 package: http://pypi.python.org/pypi/pywin32 It provides you with access to the whole WinAPI.

You could use for example COM: http://docs.activestate.com/activepython/2.7/pywin32/html/com/win32com/HTML/docindex.html

1 Comment

Use Win32. Use COM. Not very specific.
0

My own idea:

Maybe call PostMessage(WM_CopyData) in the Python script? But what's the best/standard way of calling that win32 API in Python?

3 Comments

If you did this then you wouldn't post WM_COPYDATA. In fact I'm pretty sure that would fail. You'd have to send it instead. Rob's idea is better though.
Hi David, why posting WM_COPYDATA will fail.
because it needs the data to be marshalled to the other process and I don't believe that happens for asynchronous messages.
0

I would make a simple socket server in one side, and a simple socket client on the other one. Now I can have them talking on the same computer, and also, talking across a local area network, or across the internet. Easy and fun.

In python, socket programming library options abound, as they do in Delphi. Indy is the most common solution in Delphi. Simple socket servers that are quite robust enough for you to use, are also built into python.

Also, since Python is portable beyond windows, do you really want to limit yourself to named pipes and COM and chain your Python program to windows forever?

If however, your scripts are an adjunct to the Delphi program, then Python4Delphi (ability to call Python functions and consume their results) is exactly what you need, and as far as I know, it's not well documented because it's so easy to use, it doesn't need to be. How much documentation did you read about WriteLn before you did your first WriteLn('HelloWorld')?

4 Comments

Hi Warren, socket is a good idea, but the communication will have to pass the firewall even it's on the same computer, at least with ESET Smart Security.
Regarding Python4Delphi, the version downloaded from mmm-experts.com doesn't support Python 2.7, but the latest source checked out from google code SVN server is missing several source file. I even couldn't get it to compiled...
Good point. IDLE (the python IDE) uses sockets, and it's a pain even inside a single app, that IDLE functionality
UPDATE: the author of python4delphi have just fixed the dated dependency problem, now I can compile and install the package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.