简体   繁体   中英

shared memory between c++ and python

I'm trying to connect c++(server) and python(client) using socket and want to send share data using shared memory also message sending. Data in the formate of CSV file which is created by c++.

You can do that very simply, fast and even across networks with Redis , which has bindings for C/C++. Python, Ruby, bash and so on. It is a very fast, light-weight, "in-memory" data-structure server that can serve integers, strings, lists, queues, FIFOs, sets, atomic integers, hashes, ordered sets etc. It can also run on Linux , macOS and Windows , in client or server form, without worrying about the differences in shared memory and sockets between those platforms.

The bash CLI (command-line interface) makes it very simple to debug what's going on in your code and also to inject test data.

So, for example, you could use a Redis hash to represent your data - I am just using the Terminal here to connect to the localhost, but it could be on any machine:

redis-cli -h 127.0.0.1

127.0.0.1:6379> hmset day:1 1 s 2 e 3 i 4 r
OK
127.0.0.1:6379> hmset day:5 1 e 2 i 3 r 4 r
OK
127.0.0.1:6379> hgetall day:5
1) "1"
2) "e"
3) "2"
4) "i"
5) "3"
6) "r"
7) "4"
8) "r"
127.0.0.1:6379> hmget day:5 4
1) "r"

Non-interactive version:

redis-cli --raw hmget day:5 2
i

Retrieve the day1 values with Python:

#!/usr/bin/env python3

import redis

# Redis connection
r = redis.Redis(host='localhost')

# Retrieve our hash from Redis
day1 = r.hgetall("day:1")

print(day1)

Sample Output

{b'1': b's', b'2': b'e', b'3': b'i', b'4': b'r'}

Examples of C/C++ and Python bindings here .

If you are on Windows you can try Memurai. It's a really fast-data store. It works like a charm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM