Connecting two programs

Connecting two programs
0

Its possible to connecting to programs. In the first you make the information, and then store the information in a file, and then connect to other, using the file, to make something with that?

well i don’t understand what you mean

I’d like to check if I understand the question. You want to write two programs, one that produces a data file and the other that reads from that data file and does something with the data, is that it? If that’s what you mean, yes of course it would work.

I’m not entirely sure what you mean by “connect to the other program” though. Would the first program launch the second program? Or did you just mean they connect through the data file? It’s possible for one program to call another or for two programs to communicate, although how depends on what exactly you want to do.

The first program create a data file, and the second use that data file to do something. And when the first add something to that file, the second will automatically use the file with the new information and the thing he has to do.

So you want two processes running in parallel, with one writing to file and one reading from the same file?

I’m not super-knowledgeable about this but if you want to run two programs at the same time, you’re into OS territory. You need to know a little bit about processes and/or threads.

  • You could start the two programs from two terminals and basically have them running concurrently. I guess the reading program would need to check the file at regularly intervals to see if anything changed.
  • If the reading program should only be running when the writing program is running, it could be implemented as a child process. Look into python’s multiprocessing library. Possibly you could use a pipe for the writing program to tell the reading program that an update took place.
  • You could try threads too, although I think I read somewhere that implementing threads in python is not straightforward. I don’t know much more than that.

One thing to keep in mind is that having two processes trying to access the same file at the same time, one reading, one writing, can cause a race condition. Wikipedia mentions file locking as a simple solution.

…Thanks!

I see you mentioned elsewhere that you’re only starting on your coding journey. This might be a bit tough for a beginner, simply because you already have enough on your plate learning to program and debug. Is there any way you can simplify it to run as a single program? You can always make it more complicated later.

Whatever you decide, best of luck.

Thanks. I think a good method to learn something is when learn something try upgrade, in this case the program, with the methods you learn, and if you hadn’t learn something you wanna introduce to the program, just learn it.

One more thing. How can I make a program that, for example make calculations, and the the results are stored in a file?

Are you asking how to write data to a file? Honestly it’s been years since I last programmed in python so I had to google it. This link is as good as any: https://www.jquery-az.com/python-write-to-file/. They also have a page on reading from file.

Basically you call open() with 'w' to open the file in write mode (if the file doesn’t exist it is created, if it already exists it is overwritten). open() returns a file handle that you pass to print(). Don’t forget to close the file when you’re done. If you use the with syntax it closes the file for you.

Practise writing and reading from file and decide on a format that will be easy to read (one number per line? Comma-separated? space-separated? Whatever makes it easy to read and write your data)

This is a complex topic, because there are a ton of ways to do this even within the same program. I’ll throw a bunch out here. I’ll use Linux commands Windows is a google away I figure.

Run two programs in the background that share a file reference. Write both programs, add logging as you don’t want to write to standard out using prints rather a logger, run the program with ampersand and watch file. You’ll have to write the programs as loops that write data or read data at some interval. Don’t create too many objects or write too much at once as you might exhaust memory or disk space. You’ll end up either swapping or limiting non-critical data being written in system. You might have to write a lock or do exception handling for an open file as it might error out, read a non-complete data set or create race conditions. This is kind of bare bones type stuff. You can use named pipes for this in Linux or just pipe standard out to standard in if you want to do a one time execution. If the later, you can just write a shell/batch script to execute both programs after each other (simplest solution for 1 time execution). Python has libraries for this as well in os and system I think. If you want to execute these commands every X mins/hours/days use crontab. If you are on a system that shuts off often check anacron. Generally doing things in parallel creates a lot of complexity, which you become painfully aware of when managing multiple computers :frowning:.

Another common practice, just use a database. Database is just an application for writing and reading data. Basically, you just have two clients in your system one that reads, one that writes. You just read-write as needed. Good example is MySQL, Postgres, or maybe a NoSQL solution. Ton of databases out there. Another option, slightly different is queue or stream-based solutions. You can look up queues like SQS or maybe RabbitMQ useful when you have 2 servers. More light-weight variant would be using a http server, tcp/ip connection, udp, sockets etc. Basically using internet protocols for information sharing. Less long term commitment then managing a queue or database, but can be rather complex. Some programming languages have message passing libraries. Surely python as well.

Another way is using libraries in chosen language to do IPC. In python you have multiprocessing, threading, async libraries where you can spawn threads/processes for doing things in parallel. With a bit of look-up on thread-safe options when relevant. Other languages also have similar options for sure. In this case, you can write up the program in a single file and the software itself manages everything for you. You can also use shared memory or files of you still want to write distinct programs.

Sure I’m leaving out a bunch of stuff… maybe something like erlang etc has other options (no clue). SNS in AWS.

Just google and read-up.

Sources: Certified Linux Admin, Database Admin and Software engineer working on large clusters of computers.

Hi LittleBlue,

Good question! In the most general sense what you’re talking about is “client server” computing. One program is a client, the other is the “server”. It is common nowadays to write web services, which are servers (services) implemented with and on top of web protocols. Commonly used is so called JSON based web services, which both receives requests and serves up responses in JSON format.

In any case, Python supports multiple frameworks for writing web and general services, so I’d suggest you look into doing something like this first, perhaps starting with a so-called micro-framework such as Flask or Bottle. Eventually you might look into a bigger/more general framework such as Twisted or Tornado.