简体   繁体   English

2 个 python 脚本之间的通信,其中第一个脚本持续运行

[英]Communication between 2 python script in which the first one is continuously running

I have 2 python scripts.我有 2 个 python 脚本。 To make it simple为了简单

1st script:第一个脚本:

It is a simple infinite while loop in which a variable 'x' is being increased by 1(This script is always running)这是一个简单的无限 while 循环,其中变量“x”增加 1(此脚本始终运行)

Now what I want a 2nd script, when I call this script it should give me the present value of x现在我想要第二个脚本,当我调用这个脚本时,它应该给我 x 的现值

I read about multiprocessing,pipe and queue but was not able to implement it我读到了多处理,pipe 和队列,但无法实现它

EDIT:编辑:

I tried the socket solution and I am getting errors我尝试了套接字解决方案,但出现错误

Client Side客户端

import serial
import time
from multiprocessing import Process
import sys
import socket
s=socket.socket()
port=43470
s.connect(('127.0.0.1',port))
sertx = serial.Serial('COM4', 115200)

while 1:
    for i in range(4):
        msg = str(i+1)
        # print('sending: ',msg.encode())

        msgstat = 'A' + msg
        #print(msgstat)
        #print(type(msgstat))
        tx_t = time.time()

        sertx.write(msg.encode())
        tx_t=str(tx_t)
        s.send(tx_t.encode())
        s.close()
        time.sleep(0.001)

Error - File ".\tx.py", line 23, in s.send(tx_t.encode()) OSError: [WinError 10038] An operation was attempted on something that is not a socket PS C:\Users\ambuj\Documents\Python Scripts>错误 - 文件“.\tx.py”,第 23 行,在 s.send(tx_t.encode()) 中 OSError: [WinError 10038] 尝试对不是套接字 PS C:\Users\ambuj\ 的东西进行操作文档\Python 脚本>

Server服务器

import socket

s = socket.socket() 

port = 43470 # make this any random port

s.bind(('127.0.0.1', port))

s.listen(5) # put the socket into listen mode

while True:

    c, addr = s.accept()

    data = c.recv(1024).decode("utf-8") # This data is received from the client script
    print(data)

    c.close() 

You can surely achieve this thing using socket communication.您肯定可以使用套接字通信来实现这一目标。 Just create a server script like this which will listen to any incoming data to a specific port...只需创建一个像这样的服务器脚本,它将侦听特定端口的任何传入数据......

import socket

s = socket.socket() 

port = 43470 # make this any random port

s.bind(('127.0.0.1', port))

s.listen(5) # put the socket into listen mode

while True:

    c, addr = s.accept()

    data = c.recv(1024).decode("utf-8") # This data is received from the client script

    c.close() 

Now in your client script, you have to connect to the socket that is binded in that port.现在在您的客户端脚本中,您必须连接到绑定在该端口中的套接字。 Make a client script like this...制作这样的客户端脚本...

import socket

s = socket.socket()          
port = 43470 # Use the same port number here as you did in the server script.
s.connect(('127.0.0.1', port)) 
s.send(b"This data will be received by the server!")
s.close()  

You can do the reverse as well.你也可以反过来做。 So the server will be able to send the data to the client script.因此服务器将能够将数据发送到客户端脚本。 Its a two-way communication.它是双向通信。

Remeber: This is just a simple demonstraction to make things work.记住:这只是一个简单的演示,让事情顺利进行。 In actual case, modification is much needed.在实际情况下,非常需要修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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