If there is no other option left, try using some kind of Interprocess Communication, such as sockets, as described here: https://docs.python.org/3.5/library/socket.html#example
Here is my version of the example of using sockets (written at 4 am, so don’t blame the code too much, please;)).
test2.py:
import socket f = open('log', 'w') s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind('/tmp/test_socket') s.listen(0) conn = s.accept()[0] while True: data = conn.recv(1024) if not data: break num = int(data) conn.sendall(str(num) + ':' + str(pow(num, 3))) conn.close() s.close()
test3.py:
import socket import subprocess as sp from time import sleep with sp.Popen(['python', 'test2.py']) as proc: sleep(1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.connect('/tmp/test_socket') for i in range(10): s.sendall(bytes(str(i), 'ascii')) res = str(s.recv(1024), 'utf8') print('int: {}, pow3: {}'.format(*res.split(':')))
test2.py contains code using Python 2, and test3.py - using the third version, respectively. From test3.py, test2.py is started, in which a socket is opened and a server is started, which in the loop receives one number from the client and returns the original number and its third power.
Here is the output from the test3.py file (which is the client):
int: 0, pow3: 0 int: 1, pow3: 1 int: 2, pow3: 8 int: 3, pow3: 27 int: 4, pow3: 64 и т.д.