Implementation of Python and Java interprocess communication based on Redis (subscription release)

Source: Internet
Author: User
Tags install redis

The main structure is: The Python process publishes messages, and the Java process subscribes to messages.

Dependent environment:

Python:pip Install Redis

Java:jedis

1. Python side:

pubsub.py

Import Redisclass PubSub (object):    def __init__ (self, host, port, db):        self.__conn = Redis. Redis (host, port, DB)    def publish (self, Channel, msg):        self.__conn.publish (channel, msg)        return True    def subscribe (self, channel):        pub = Self.__conn.pubsub ()        pub.subscribe (channel)        Pub.parse_response ()        return Pub

sub.py

From PubSub Import pubsubobj = PubSub (' localhost ', 6379, 1) redis_sub = obj.subscribe (' cord ') while True:    msg = REDIS_SU B.parse_response ()    msg = Msg[2].decode ()    print (msg)

2. Java side

Redispub.java

Import redis.clients.jedis.jedis;import java.util.date;public class redispub {    private static Jedis Jedis = new Jedis ("localhost", 6379);    Private static final String channel = "cord";    public static void main (string[] args) {String message = new Date (). toString (); Jedis.publish (channel, message) ; }}

Reference Links:

Http://www.cnblogs.com/melonjiang/p/5342383.html

Implementation of Python and Java interprocess communication based on Redis (subscription release)

Related Article

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.