Python implements simple inter-process communication instances, and python implements process instances.

Source: Internet
Author: User

Python implements simple inter-process communication instances, and python implements process instances.

This article describes how to implement communication between two programs in python. The specific method is as follows:

This instance is implemented using socket. The first parameter of socket. socket (socket. AF_UNIX, socket. SOCK_STREAM) is socket. AF_UNIX, which is different from socket network programming.
Instead of socket. AF_INET
In this example, the two python programs s. py/c. py must first run s. py
Tested Based on fedora13/python2.6 and implemented successfully!

The s. py code is as follows:

#!/usr/bin/env pythonimport socketimport osif __name__ == '__main__':  sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  conn = '/tmp/conn'  if not os.path.exists(conn):  os.mknod(conn)  if os.path.exists(conn):  os.unlink(conn)  sock.bind(conn)  sock.listen(5)  while True:  connection,address = sock.accept()  data = connection.recv(1024)  if data == "hello,server":    print "the client said:%s!\n" % data      connection.send("hello,client")  connection.close() 

The c. py code is as follows:

#!/usr/bin/env pythonimport socketimport timeif __name__ == '__main__':  sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  conn = '/tmp/conn'  sock.connect(conn)  time.sleep(1)  sock.send('hello,server')  print sock.recv(1024)  sock.close() 

How to compile a java library and provide an api that allows others to call this library using python, for example, to return a simple string

Java and python are different processes. You need to communicate with each other. A common method for inter-process communication is through the network, such as using tcp.
Currently, common cross-language RPC solutions include ZeroMQ + protobuf and Thrift.

If you are a newbie, I can only say that different languages cannot call each other at will. You can only use methods for inter-process communication.

Another way to avoid inter-process communication is to use Java's JNI as a dll, and then use Python's ffi to call this dll. In this way, the code is in the same process, and you need to learn JNI and Python CTypes.

Help me write a C # example: communication between two processes

// Client:

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
// Add namespace reference
Using System. Net;
Using System. Net. Sockets;
Using System. Threading;
Using System. IO;

Namespace ChatClient
{
Public partial class FormClient: Form
{
Private bool isExit = false;
Private delegate void SetListBoxCallback (string str );
Private SetListBoxCallback setListBoxCallback;
Private TcpClient client;
Private NetworkStream networkStream;
Private BinaryReader br;
Private BinaryWriter bw;
Public FormClient ()
{
InitializeComponent ();
ListBoxStatus. HorizontalScrollbar = true;
SetListBoxCallback = new SetListBoxCallback (SetListBox );
}
Private void buttonConnect_Click (object sender, EventArgs e)
{
Try
{
// Change Dns. GetHostName () to the server domain name in actual use.
Client = new TcpClient (Dns. GetHostName (), 51888 );
SetListBox (string. Format ("Local EndPoint: {0}", client. Client. LocalEndPoint ));
SetListBox ("Connection established with server successful ");
}
Catch
{
SetListBox ("failed to connect to server ");
Return;
}
ButtonConnect. Enabled = false;
// Obtain the network stream
NetworkStream = client. GetStrea... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.