简体   繁体   English

在Python中同时使用多个程序

[英]Using multiple programs simultaneously in Python

I'm fairly new to Python and I'm trying to write a script to automate a test. 我对Python还是很陌生,我正在尝试编写一个脚本来自动化测试。

How it works: Program A: Sends commands through serial port waits for response and then executes next command Program B: Uses a TCP_Client.exe app using Subprocess module to send arguments (ip, port, number of packets, size) along with the app to the command prompt and reads output. 工作原理:程序A:通过串行端口发送命令以等待响应,然后执行下一条命令。程序B:使用带有Subprocess模块​​的TCP_Client.exe应用程序与该应用程序一起发送自变量(ip,端口,数据包数量,大小)到命令提示符并读取输出。

What its supposed to do: 它应该做什么:

  1. start tcp server on device accessed through serial port (Pyserial module) ===========> Program A 在通过串行端口(Pyserial模块)访问的设备上启动tcp服务器===========>程序A
  2. send "tcpclient.exe ..." to command prompt. 发送“ tcpclient.exe ...”到命令提示符。 This will bind the socket and then prompt me to send some more commands through the serial port to the device. 这将绑定套接字,然后提示我通过串行端口向设备发送更多命令。 ==========> Program B ==========>程序B
  3. Send additional Commands to the device ==========> Program A 将其他命令发送到设备===========>程序A
  4. Enter a number to continue tcpclient.exe to next stage where packets are sent. 输入数字以继续tcpclient.exe到下一个发送数据包的阶段。 ===========> Program B. ===========>程序B。
  5. Wait for packets to be sent. 等待数据包发送。 Program sends a prompt saying done but does not quit. 程序发送提示说完成但不退出的提示。 Waits for me to read data on device ===========> Program B 等待我读取设备上的数据===========>程序B
  6. Send command through serial port to read data ==========> Program A 通过串口发送命令以读取数据==========>程序A
  7. Go back to tcpclient.exe and quit the program. 返回到tcpclient.exe并退出程序。 (basically need to hit a number followed by to continue to finish. (基本上需要先打一个数字,然后再继续才能完成操作。

RunSer2Command(lines2[21])
time.sleep(1)       
ls_output = subprocess.Popen(['tcpclient.exe','192.168.22.33','5000','100000','1400'],stdout=subprocess.PIPE)
time.sleep(1)
RunSer2Command(lines2[22])
RunSer2Command(lines2[23])
time.sleep(1)
ls_output = subprocess.Popen(['3'],stdout = subprocess.PIPE)
ls_output.communicate()

RunSer2Command(lines2[24])

ser2.close()

Something Like this 像这样

Could someone tell me if I should read up multi threading or is this too small to require it? 有人可以告诉我是否应该阅读多线程,或者它太小而不能要求吗? If so what should I be looking for? 如果是这样,我应该找什么?

Thanks in advance 提前致谢

Answering my own question. 回答我自己的问题。 Short answer is Threading will do it. 简短的答案是线程可以做到。 It is also unnecessary. 这也是不必要的。 The subprocess module has enough for me to make it work. 子流程模块足以让我正常工作。 I just wasn't doing it right 我只是做得不好

RunSer2Command(lines2[21])
time.sleep(1)   
ls_output = subprocess.Popen(['tcpclient.exe','192.168.4.110','8000','10000','1400'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,bufsize=3)
time.sleep(2)
RunSer2Command(lines2[22])
RunSer2Command(lines2[23])
time.sleep(1)
ls_output.communicate(input = '3')
ls_output.wait()
RunSer2Command(lines2[24])

For those who care threading route did get me to a certain point and I'll add that but I didn't go to the last condition since well... I found the easier route 对于那些在意穿线路线的人确实使我到达了某个点,我会补充一点,但自那以后我并没有走到最后一个条件...我发现了更简单的路线

def start_tcp_client(cond): 
    ls_output = subprocess.Popen(['tcpclient.exe','192.168.4.110','8000','1000','1400'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,bufsize=3)
    with cond:
        cond.wait()
        ls_output.communicate(input = '3')
        ls_output.communicate()

def TCPSettings(cond):
    with cond:
        RunSer2Command(lines2[22])
        RunSer2Command(lines2[23])
        cond.notify()

    condition = threading.Condition()
    condition1 = threading.Condition()
    Client_thread=threading.Thread(name='Client_thread', target=start_tcp_client, args=(condition,))
    TCP_thread=threading.Thread(name='TCP_thread', target=TCPSettings, args=(condition,))
    RunSer2Command(lines2[21])
    time.sleep(2)   
    Client_thread.start()
    time.sleep(2)
    TCP_thread.start()
    time.sleep(1)
    Client_thread.join()
    time.sleep(10)
    RunSer2Command(lines2[24])

I still havent managed to work out all the kinks. 我仍然没有设法解决所有问题。 There appear to be some timing issues. 似乎存在一些计时问题。 I'll update it once I get it working perfectly. 一旦它可以正常运行,我将对其进行更新。

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

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