python多处理变量

python多处理变量,python,Python,我如何在同一项目的另一个过程中使用从一个过程中获得的数据(变量)?例如,在下面的代码中,我想在一个界面中使用变量“b”来绘制和分析它。谢谢 import time import multiprocessing import time import os import sys import matplotlib.animation as animation import time import libtiepie import numpy as np import matplotlib.pyplo

我如何在同一项目的另一个过程中使用从一个过程中获得的数据(变量)?例如,在下面的代码中,我想在一个界面中使用变量“b”来绘制和分析它。谢谢

import time
import multiprocessing
import time
import os
import sys
import matplotlib.animation as animation
import time
import libtiepie
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy
from printinfo import *
from math import*
import pylab
import tkinter as tk
import matplotlib.animation as animation
from tkinter import*
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib.animation import FuncAnimation

def calc_square():

    k=0
    fig=plt.figure()
    while k<3000:

        # Print library info:
        print_library_info()

        # Search for devices:
        libtiepie.device_list.update()

        # Try to open an oscilloscope with block measurement support:
        scp = None
        for item in libtiepie.device_list:
            if item.can_open(libtiepie.DEVICETYPE_OSCILLOSCOPE):
                scp = item.open_oscilloscope()
                if scp.measure_modes & libtiepie.MM_BLOCK:
                    break
                else:
                    scp = None

        if scp:
            try:

                    # Set measure mode:
                    scp.measure_mode = libtiepie.MM_BLOCK

                    # Set sample frequency:
                    scp.sample_frequency = 5e6  # 1 MHz

                    # Set record length:
                    scp.record_length = 1000  # 15000 samples

                    # Set pre sample ratio:
                    scp.pre_sample_ratio = 0.1  # 0 %

                    # For all channels:
                    for ch in scp.channels:
                        # Enable channel to measure it:
                        ch.enabled = True

                        # Set range:
                        ch.range = 8  # 8 V

                        # Set coupling:
                        ch.coupling = libtiepie.CK_ACV  # DC Volt

                    # Set trigger timeout:
                    scp.trigger_time_out = 100e-3  # 100 ms

                    # Disable all channel trigger sources:
                    for ch in scp.channels:
                        ch.trigger.enabled = False

                    # Setup channel trigger:
                    ch = scp.channels[0]  # Ch 1

                    # Enable trigger source:
                    ch.trigger.enabled = True

                    # Kind:
                    ch.trigger.kind = libtiepie.TK_RISINGEDGE  # Rising edge

                    # Level:
                    ch.trigger.levels[0] = 0.75  # 50 %

                    # Hysteresis:
                    ch.trigger.hystereses[0] = 0.1  # 5 %

                    # Print oscilloscope info:
                    #print_device_info(scp)

                    # Start measurement:
                    scp.start()

                    # Wait for measurement to complete:
                    while not scp.is_data_ready:
                        time.sleep(0.01)  # 10 ms delay, to save CPU time

                    # Get data:
                    data = scp.get_data()

                    ax = fig.add_subplot(111)
                    t = np.linspace(0, (scp.record_length/scp.sample_frequency)*1000, scp.record_length)
                    a=deepcopy(data)
                    a=np.transpose(a)
                    b=a[:,0]
                    #ax.plot(t,b)
                    #plt.ion()
                    #plt.show()
                    #plt.pause(0.001)
                    #ax.cla()

            except Exception as e:
                print('Exception: ' + e.message)
                sys.exit(1)

            # Close oscilloscope:
            del scp

        else:
            print('No oscilloscope available with block measurement support!')
        k=k+1

if __name__ == "__main__":

    p1 = multiprocessing.Process(target=calc_square)      

    p1.start()
    # p2.start()
    p1.join()
    # p2.join()
导入时间
导入多处理
导入时间
导入操作系统
导入系统
将matplotlib.animation导入为动画
导入时间
导入libtiepie
将numpy作为np导入
将matplotlib.pyplot作为plt导入
从复制导入deepcopy
从printinfo导入*
从数学导入*
进口派拉布
将tkinter作为tk导入
将matplotlib.animation导入为动画
从tkinter进口*
从tkinter导入ttk
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG,导航工具栏2TKAGG
从matplotlib.figure导入图形
从matplotlib.animation导入FuncAnimation
def calc_square():
k=0
图=plt.图()

当k要将数据从一个进程发送到另一个进程时,可以使用多处理队列,如


下面是将一个进程的变量传递给另一个进程的工作方式

只是为了清楚,您有两个线程,thread1返回一个变量,是否要将此变量传递给另一个thread2函数?此示例提供错误:RuntimeError:在当前进程完成其引导阶段之前,已尝试启动新进程。这可能意味着您没有使用fork启动子进程,并且忘记在主模块中使用正确的习惯用法:if name==“main”:freeze_support()。。。如果程序不会被冻结以生成可执行文件,则可以省略“freeze_support()”行。代码对我和在线解释器都很好。我在这里省略了if name=='main',因为在在线平台上,如果导入了所有代码,那么它就不会运行,因此我在这里跳过了它。
from multiprocessing import Process, Queue
q = Queue()

def function1(q):
  a = 3434
  q.put(a)
def function2(a):
  c = a**2
  q.put(c)
p1 = Process(target=function1, args=(q,))
p1.start()
b = q.get()
p1.join()
p2 = Process(target=function2, args=(b,))
p2.start()
c = q.get()
p2.join()
print c