Python inter-process communication instance, python Process

Source: Internet
Author: User

Python inter-process communication instance, python Process

Python implements simple inter-process communication instances

The example explains how python implements communication between two programs. 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 python
Import socket
Import OS

If _ 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 python
# Www.jbxue.com
Import socket
Import time

If _ 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 ()


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>

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.

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.