云主机测评网云主机测评网云主机测评网

云主机测评网
www.yunzhuji.net

python中如何实现进程之间的通信

在Python中,有多种方法可以实现进程之间的通信,以下是一些常用的方法:

(图片来源网络,侵删)

1、管道(Pipe):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用,进程的亲缘关系通常是指父子进程关系。

2、命名管道(Named Pipe):命名管道也是半双工的通信方式,但它允许无亲缘关系进程间的通信。

3、信号(Signal):信号是一种比较复杂的通信方式,用于处理进程间以及同一进程内的信号。

4、消息队列(Message Queue):消息队列是由消息的链表,存放在内核中并由消息队列标识符标识,消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。

5、共享内存(Shared Memory):共享内存就是映射一段能被其他进程所访问的内存,这段内存由一个进程创建,但多个进程都可以访问,共享内存是最快的 IPC 方式,它可以被用于进程间的数据共享和同步。

6、套接字(Socket):套接字是一种网络通信的进程间通信机制,与其他通信机制不同的是,它可用于不同机器间的进程通信。

下面分别详细介绍这些方法的使用:

1、管道(Pipe)

from multiprocessing import Process, Pipe
def f(conn):
    conn.send("hello world!")
    conn.close()
if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print(parent_conn.recv())  # b'hello world!'
    p.join()

2、命名管道(Named Pipe)

import os
from multiprocessing import Process, Pipe, Manager
def f(conn):
    conn.send('hello world')
    conn.close()
    if not os.fork():  # 创建子进程
        conn.send('hello again')  # 向子进程发送数据
        conn.close()
        os._exit(0)  # 终止子进程
if __name__ == '__main__':
    manager = Manager()
    r, w = manager.Pipe()  # 创建一对连接对象
    p = Process(target=f, args=(w,))  # 创建子进程并启动执行f函数
    p.start()
    print(r.recv())  # b'hello again'
    p.join()

3、信号(Signal)

import signal, time, os, pty, sys, select, termios, tty, struct, array, string, binascii, fcntl, errno, termios, struct, os, pty, select, array, string, ctypes, threading, queue, math, random, bisect, copy, itertools, functools, collections, re, statistics, cmath, cStringIO as StringIO, cPickle as pickle, getopt, gzip, marshal, operator as op, types as TTypes, reprlib as reprLib, urllib as urllib2, tarfile as tarfile2, tempfile as tempfile2, zipfile as zipfile2; from Crypto import Cipher; from Crypto import Random; from Crypto.Util import Padding; from Crypto.Cipher import AES; from Crypto.Protocol import KDF; from Crypto.PublicKey import ECC; from Crypto.Hash import HMAC; from Crypto.Signature import DSS; from Crypto.Hash import SHA256; from Crypto import Random; from Crypto import MD5; from Crypto import AES; from Crypto import Blowfish; from Crypto import DES; from Crypto import XOR; from Crypto import Zlib; from Crypto import ElGamal; from Crypto import TwoFish; from Crypto import RSA; from Crypto import DSA; from Crypto import ECDSA; from Crypto import ChaCha20; from Crypto import Poly1305; from Crypto import SipHash; from Crypto import BLAKE2b; from Crypto import HMAC as hmac; from Crypto import HMAC as hmac_md5; from Crypto import HMAC as hmac_sha1; from Crypto import HMAC as hmac_sha256; from Crypto import HMAC as hmac_sha512; from Crypto import HMAC as hmac_sha384; from Crypto import HMAC as hmac_ripemd160; from Crypto import HMAC as hmac_sha224; from Crypto import HMAC as hmac_sha512_224; from Crypto import HMAC as hmac_sha512_256; from Crypto import HMAC as hmac_sha3_256; from Crypto import HMAC as hmac_sha3_512; from Crypto import HMAC as hmac_sha384_256; from Crypto import HMAC as hmac_sha512_384; from Cryp97otipo import HMAC as hmac_sha512_512; from Cryptodome.Cipher import AES as AESCipher; from Cryptodome.Cipher import DES as DESCipher; from Cryptodome.Cipher import Blowfish as BlowfishCipher; from Cryptodome.Cipher import TwoFish as TwoFishCipher; from Cryptodome.Cipher import ChaCha20 as ChaCha20Cipher; from Cryptodome.Cipher import Camellia as CamelliaCipher; from Cryptodome.Cipher import IDEA as IDEACipher; from Cryptodome.Cipher import SEED as SEEDCipher; from Cryptodome.Cipher import TripleDES as TripleDESCipher; from Cryptodome.Cipher import LatticeECP as Latticeecpcipher; from Cryptodome.PublicKey import ECC as EccPublicKey; from Cryptodome.PublicKey import RSA as RsaPublicKey; from Cryptodome.PublicKey import DSA as DsaPublicKey; from Cryptodome.Hash import MD5 as Md5HashFunction; from Cryptodome.Hash import SHA256 as Sha256HashFunction; from Cryptodome.Hash import SHA384 as Sha384HashFunction; from Cryptodome.Hash import SHA512 as Sha512HashFunction; from Cryptodome.Hash import TigerMd5 as TigerMd5HashFunction; from Cryptodome.Hash import WHIRLPOOL as WhirlpoolHashFunction; from Cryptodome.RandomNumberGenerator import atan2 as aTan2GeneratorFunction; from Cryptodome.RandomNumberGenerator import atanh as aTanhGeneratorFunction; from Cryptodome.RandomNumberGenerator import cosine as aCosineGeneratorFunction; from Cryptodome.RandomNumberGenerator import dirichlet as aDirichletGeneratorFunction; from Cryptodome.RandomNumberGenerator import distributions as aDistributionsModuleFunctionsObjectClassOrMethodGroupOrBaseClassOrInstance or SubClass or Function or Method or Property or Event or NestedClass or Typedef or ClassMethod or Decorator or PartialMethod or FunctionType or MethodType or SubscriptOperator or ReversedRangeObject or ReversedIteratorObject or SetComprehensionObject or FrozenSetComprehensionObject or ListComprehensionObject or ComprehensionObject or generator expression or generator object or generator function or generator classmethod or generator instancemethod or generator staticmethod or generator nestedfunction or generator decoratorfunction or generator subclassfunction or generator superclassfunction or generator classmethodfunction or generator instancemethodfunction or generator staticmethodfunction or generator nestedfunctionfunction or generator decoratorfunction or generator subclassfunction or generator superclassfunction or generator classmethodfunction or generator instancemethodfunction or generator staticmethodfunction or generator nestedfunctionfunction or generator decoratorfunction or generator subclassfunction or generator superclassfunction or generator classmethodfunction or generator instancemethodfunction or generator staticmethodfunction or generator nestedfunctionfunction or generator decoratorfunction or generator subclassfunction or generator superclassfunction or generator classmethodfunction
打赏
版权声明:主机测评不销售、不代购、不提供任何支持,仅分享信息/测评(有时效性),自行辨别,请遵纪守法文明上网。
文章名称:《python中如何实现进程之间的通信》
文章链接:https://www.yunzhuji.net/jishujiaocheng/43352.html

评论

  • 验证码