Matplot real time plotting cpu load - python

I want to display CPU using mat plot library. My python code sends cpu load for every 1 sec through serial port to target board and output i need to display it using plot. i came across some examples but I'm not getting how to integrate in my code. I'm new to python. This is the code I've written.
import serial, time, threading, multiprocessing,sys
import numpy as np
from matplotlib import pyplot as plt
from collections import deque
import csv
ser = serial.Serial(
port='COM4',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
class analogData:
# constr
def __init__(self, maxLen):
self.ax = deque([0.0]*maxLen)
self.ay = deque([0.0]*maxLen)
self.maxLen = maxLen
# ring buffer
def addToBuf(self, buf, val):
if len(buf) < self.maxLen:
buf.append(val)
else:
buf.pop()
buf.appendleft(val)
# add data
def add(self, data):
assert(len(data) == 2)
self.addToBuf(self.ax, data[0])
self.addToBuf(self.ay, data[1])
# plot class
class analogPlot:
# constr
def __init__(self, analogData):
# set plot to animated
plt.ion()
self.axline, = plt.plot(analogData.ax)
self.ayline, = plt.plot(analogData.ay)
plt.ylim([0, 1023])
plt.show()
# update plot
def update(self, analogData):
self.axline.set_ydata(analogData.ax)
self.ayline.set_ydata(analogData.ay)
plt.draw()
def serialWrite(cmd: str):
l=0
ser.flushOutput()
time.sleep(0.04)
while l < len(cmd):
ser.write(cmd[l].encode())
time.sleep(0.02)
ser.flushInput()
l=l+1
ser.write('\r'.encode())
def serialRead():
time.sleep(0.065)
out=""
while ser.inWaiting() > 0:
out += ser.read().decode("utf-8")
return out
def read_from_serial():
#pool_sema.acquire()
out=""
serialWrite("cpu")
out=serialRead()
data = [float(val) for val in out.split()]
analogData.add(analogData,data)
analogPlot.update(analogData)
print (data)
#pool_sema.release()
threading.Timer(1, read_from_serial).start()
def commandInput():
while True:
out=""
out=input()
serialWrite(out)
out=""
out=serialRead()
print (out)
if __name__ == "__main__":
serial_thread = threading.Thread(target = read_from_serial())
main_thread=threading.Thread(target = commandInput())
analogData = analogData(100)
analogPlot = analogPlot(analogData)
serial_thread.start()
main_thread.start()
Please give me suggestions what are things I'm doing wrong.

Related

Python Program Loses Track of Time

[EDIT]
I've added an incremental counter to the data, to see if I'm missing some data, I don't miss data. These are the times between packets in nanoseconds:
650255.8786692759 (Agrees with the oscilloscope)
500667.7866927593 (wrong)
491091.92954990215 (wrong)
484505.240704501 (wrong)
488062.3091976517 (wrong)
487157.1350293542 (wrong)
485264.33268101764 (wrong)
I have a Python Program that reads the serial port. And draws it on a Chart. Using Qt.
The data comes in every 300 microseconds (Confirmed with an Oscilloscope).
Problem is after the first run through of the worker Thread (traad()), it says the time is 100 microseconds between reads. (which is clearly wrong)
I just can't figure out what's wrong!
import serial
from PyQt5 import QtWidgets
from pyqtgraph.Qt import QtCore
import pyqtgraph as pg
import sys
from scipy.fft import fft, fftfreq
import numpy as np
import threading
import time
import re
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.lock = threading.Lock()
self.Voltage = []
self.tid = []
self.xdata = []
self.ydata = []
self.dataReady = False
self.dataRead = True
self.samples = 512
self.x = threading.Thread(target=self.traad, args=(self.samples,))
self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)
self.x.start()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(100)
def traad(self, s):
ser = serial.Serial('/dev/ttyUSB0', baudrate=576000, stopbits=2, bytesize=8)
while True:
while self.dataRead:
self.dataRead = False
self.lock.acquire()
self.xdata = []
self.ydata = []
self.Voltage = []
self.tid = []
counter = 0
self.lock.release()
while counter < s:
if ser.read(1) == b'?':
data = ser.readline().strip()
stid = time.time_ns()
data = re.search(b"\\d+\\.\\d+", data)
if data:
self.lock.acquire()
self.Voltage.append(data.group(0))
self.tid.append(stid)
self.lock.release()
print(data.group(0))
counter += 1
tot = self.get_samplerate(self.tid)
xf, yf = self.CalcFft(self.Voltage, tot)
self.lock.acquire()
self.xdata = xf
self.ydata = yf
self.dataReady = True
self.lock.release()
def CalcFft(self, volt, deltatid):
N = len(volt)
T = deltatid / 1000000000
yf = fft(volt)
xf = fftfreq(N, T)[:N // 2]
return xf, yf
def get_samplerate(self, tider):
total = []
delta = 0
test = 0
for x in tider:
total.append(x-test)
test = x
total.pop(0)
for e in total:
delta += e
l = len(total)
d = delta / l
print(d)
return d
def updater(self):
if self.dataReady:
self.graphWidget.plot(self.xdata, 2.0 / self.samples * np.abs(self.ydata[0:self.samples // 2]))
self.lock.acquire()
self.dataReady = False
self.dataRead = True
self.lock.release()
def main():
app = QtWidgets.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Read an array from a UDP port in Python

I have the following Simulink model:
Simulink Model with UDP
With this model and a joystick I want to provide inputs to the UDP port which I will read from another application; in my case Blender.
So at Blender I have the following code that works perfectly for 1 provided input from Simulink.
import bpy
import math
import socket
import struct
port = 12009
address = "127.0.0.1"
base = 0
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((address, port))
cube = bpy.data.objects['Cube']
cube.location = (base, 0, 0)
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
s.close()
cube.location = (base, 0, 0)
return {'CANCELLED'}
if event.type == 'TIMER':
data, addr = s.recvfrom(1024)
data = struct.unpack('!d', data)
x = data[0]
print(data)
cube.location = (x, 0, 0)
print("X:", x)
return {'PASS_THROUGH'}
def execute(self, context):
wm = context.window_manager
self._timer = wm.event_timer_add(0.001, window = context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
def register():
bpy.utils.register_class(ModalTimerOperator)
def unregister():
bpy.utils.unregister_class(ModalTimerOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.modal_timer_operator()
When I try to run this, I get the following error:
struct.error: unpack requires a buffer of 8 bytes
How can I unpack an array instead of just a double using struct.unpack?
What changes should I make in the code to achieve this?
I found the answer myself.
There is no need to use matrix concatenation; just simply connect the mux singal of your desired sent signals via UDP. Then in the code just change the line of the struct.unpack to the following (if you have 3 signals):
data = struct.unpack('!ddd', data)
x = data[0]*100
y = data[1]*100
z = data[2]*100

Python update Matplotlib from threads

I'm pretty new to the python world and unfortunately I could not find any solution to this yet.
I'm running python 3.6 with matplotlib==1.3.1 on Mac OS X 10.13.2
Currently I'm trying to build a small software which fetches data at 4Hz and displays the fetched data in a plot at 1Hz. Therefore I created a class which runs in a thread to fetch the data and another class to update the actual plots. In-between there is a data class which will hold the data and is used as interface inbetween the two classes.
import matplotlib.pyplot as plt
import numpy as np
import threading
import random
import time
class MyDataClass():
def __init__(self):
self.XData = [0]
self.YData = [0]
class MyPlotClass(threading.Thread):
def __init__(self, dataClass):
threading.Thread.__init__(self)
self._dataClass = dataClass
self._period = 1
self._nextCall = time.time()
self.hLine, = plt.plot(0, 0)
plt.ion()
def run(self):
while True:
self.hLine.set_data(self._dataClass.XData, self._dataClass.YData)
plt.draw()
print("updated %i datapoints" % len(self._dataClass.XData))
# sleep until next execution
self._nextCall = self._nextCall + self._period
time.sleep(self._nextCall - time.time())
class MyDataFetchClass(threading.Thread):
def __init__(self, dataClass):
threading.Thread.__init__(self)
self._dataClass = dataClass
self._period = 0.25
self._nextCall = time.time()
def run(self):
while True:
# add data to data class
self._dataClass.XData.append(self._dataClass.XData[-1] + 1)
self._dataClass.YData.append(random.randint(0, 256))
print("Added (%i, %i)" % (self._dataClass.XData[-1], self._dataClass.YData[-1]))
# sleep until next execution
self._nextCall = self._nextCall + self._period
time.sleep(self._nextCall - time.time())
data = MyDataClass()
fetcher = MyDataFetchClass(data)
plotter = MyPlotClass(data)
fetcher.start()
plotter.start()
fetcher.join()
plotter.join()
I can see that the threads are running due to the command line output. But for some reason the figure holding the plots won't show up.
The rocket symbol will just bounce up and down instead of showing up. See attached Screenshot.
Simple examples where the plot is only created ones and the plt.show() command is used works fine.
I can't figure out what I'm doing wrong. I hope anyone of you might have an idea.
Thanks!
Edit: Solutions presented here How to update a plot in matplotlib? will not work for me because I don't want to be limited to a certain number of frames (using the animation framework of Matplotlib). I need to update the plot with 1Hz continiously.
Figure not showing up
I don't think you can run matplotlib GUI outside the main thread. So keeping the plotting in the main thread and using a FuncAnimation to steer the plotting, the following seems to work fine.
Due to the while True loop it will run forever, even after closing the window, so for any real world application this should still be adjusted.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import threading
import random
import time
class MyDataClass():
def __init__(self):
self.XData = [0]
self.YData = [0]
class MyPlotClass():
def __init__(self, dataClass):
self._dataClass = dataClass
self.hLine, = plt.plot(0, 0)
self.ani = FuncAnimation(plt.gcf(), self.run, interval = 1000, repeat=True)
def run(self, i):
print("plotting data")
self.hLine.set_data(self._dataClass.XData, self._dataClass.YData)
self.hLine.axes.relim()
self.hLine.axes.autoscale_view()
class MyDataFetchClass(threading.Thread):
def __init__(self, dataClass):
threading.Thread.__init__(self)
self._dataClass = dataClass
self._period = 0.25
self._nextCall = time.time()
def run(self):
while True:
print("updating data")
# add data to data class
self._dataClass.XData.append(self._dataClass.XData[-1] + 1)
self._dataClass.YData.append(random.randint(0, 256))
# sleep until next execution
self._nextCall = self._nextCall + self._period;
time.sleep(self._nextCall - time.time())
data = MyDataClass()
plotter = MyPlotClass(data)
fetcher = MyDataFetchClass(data)
fetcher.start()
plt.show()
#fetcher.join()

GUI with pyqtgraph never refresh

I have been working on a GUI for the beagle bone black that launch a thread when a button is clicked and starts to get data through the SPI.
This function is inside a class called Scanner(QObject) and runs in a different thread when the Start button is clicked.
def scan (self):
thread_name = QThread.currentThread().objectName()
self.sig_msg.emit('Scanning '+thread_name)
for step in range(nsamples):
data = self.read_reg(reg[thread_name])
self.sig_data.emit(step, data)
QThread.currentThread().msleep(50)
app.processEvents()
if self.__abort:
self.sig_msg.emit('scan stopped by user')
break
self.sig_done.emit(thread_name)
sig_msg is a pyqtsignal connected to the following function inside the GUI thread.
#pyqtSlot(int, int)
def on_scaner_data(self, t: int, y: int):
app.processEvents()
self.debugBox.insertPlainText('t: '+str(t)+'y: '+str(y)+'\n')
self.debugBox.ensureCursorVisible()
self.MainGraph.update_fig(t,y)
And finally the MainGraph.update_fig() is called. Inside that function i have used setData(self.datat,self.datay) and app.processEvents() for update the graph, but nothing changes. If i run plot(self.datat,self.datay) instead it redraws the graph but causes a huge performance hit.
class DynamicPlotter(PlotWidget):
def __init__(self,parent=None):
PlotWidget.__init__(self)
self.setParent(parent)
# Use getPlotItem() to get the PlotItem inside PlotWidget.
self.pitem = self.getPlotItem()
#now pitem is our PlotItem
self.pitem.curve=self.pitem.plot()
#curve is a new PlotDataItem added by PlotItem.plot()
self.datat = [1,2]
self.datay = [1,2]
self.pitem.curve.setData(self.datat,self.datay)
#this graph works fine
self.datat = []
self.datay = []
def update_fig(self,t:int,y:int):
self.datat.append(t)
self.datay.append(y)
#it works
self.pitem.curve=self.pitem.plot(self.datat,self.datay)
#it doesn't
self.pitem.curve.setData(self.datat,self.datay)
app.processEvents()
print (self.datat)
log.debug(str(t)+str(y))
def reset_figure(self):
log.debug('clean graph')
self.clear()
I have been following this example from the pyqtplot and my idea was do something similar inside my GUI.
import initExample
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
app = QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('pyqtgraph example: PlotSpeedTest')
p.setRange(QtCore.QRectF(0, -10, 5000, 20))
p.setLabel('bottom', 'Index', units='B')
curve = p.plot()
data = np.random.normal(size=(50,5000))
ptr = 0
lastTime = time()
fps = None
def update():
global curve, data, ptr, p, lastTime, fps
curve.setData(data[ptr%10])
ptr += 1
now = time()
dt = now - lastTime
lastTime = now
if fps is None:
fps = 1.0/dt
else:
s = np.clip(dt*3., 0, 1)
fps = fps * (1-s) + (1.0/dt) * s
p.setTitle('%0.2f fps' % fps)
app.processEvents() ## force complete redraw
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
I have been reading the documentation and right know I'm not sure where is/are the problems. I bet for the threads or the event loop handler but i don't know.
Which are the critical points that i have to review?
Any clue?
Thank you.
After a while i have found the problem by myself.
I fixed the problem changing the way that i was using to reset the graph and stopping the scan thread until elements are plot.
Changes on reset function. self.clear() remove the traces on the PlotWidget and that wasn't what i needed.
def reset_figure(self):
log.debug('clean graph')
self.datat =[]
self.datay=[]
self.pitem.curve.setData(self.datat,self.datay)
Scan was modified to stop while the data is plotted in the other thread. sync_to_plot stop the thread execution until self._wait=False. This value is changed by wait_state.
def scan (self):
thread_name = QThread.currentThread().objectName()
#thread_id = str(QThread.currentThreadId())#review
self.sig_msg.emit('Scanning '+thread_name)
for step in range(nsamples):
data = self.read_reg(reg[thread_name])
self.sig_data.emit(step, data)
#pause while plot values
self.sync_to_plot()
if step % refrate == 0:
log.debug("%5d : %d" % (step, data) )
if self.__abort:
self.sig_msg.emit('scan stoped by user')
break
self.sig_done.emit(thread_name)
def sync_to_plot(self):
self._wait=True
while self._wait:
log.debug("waiting")
QThread.currentThread().msleep(1)
app.processEvents()
def wait_state(self, stat):
self._wait=stat
With that done the last change was on the on_scaner_data that unblocks the thread that is waiting on sync_to_plot.
#pyqtSlot(int, int)
def on_scaner_data(self, t: int, y: int):
app.processEvents()
self.debugBox.insertPlainText('t: '+str(t)+'y: '+str(y)+'\n')
self.debugBox.ensureCursorVisible()
self.MainGraph.update_fig(t,y)
for thread, scaner in self.__threads:
scaner.wait_state(False)
log.debug("scanner false")

I have a serial Python application that takes hours to process, how can I decrease the time it takes to run?

Could someone please post a few examples of multi-threaded python? I am searching all over the internet but cannot find a simple, easy-to-replicate tutorial. Simple examples are fine.
I have written a program which takes a few hours to run serially--I am hoping I can bring it's run time down to minutes after multi-threading it.
I see you got a lot of examples, all so far from #Noctis, but I'm not sure how they're going to help you. Addressing your question more directly: the only way multithreading can speed your application up, in today's CPython, is if your slow-down is due in good part to "blocking I/O" operations, e.g. due to interactions with (for example) DB servers, mail servers, websites, and so on. (A powerful alternative to speed up I/O is asynchronous, AKA event-driven, programming, for which the richest Python framework is twisted -- but it can be harder to learn, if you've never done event-driven coding).
Even if you have many cores in your machine, one multi-threaded Python process will use only one of them at a time, except when it's executing specially coded extensions (typically in C, C++, Cython, and the like) which "release the GIL" (the global interpreter lock) when feasible.
If you do have many cores, multiprocessing (a module whose interface is designed to look a lot like threading) can indeed speed up your program. There are many other packages supporting "symmetric multi-processor" distributed programming, see the list here, but, out of all of them, multiprocessing is the one that comes as part of the standard library (a very convenient thing). If you have multiple computers with a fast LAN between them, you should also consider the more general approach of distributed processing, which could let you use all of your available computers for the same task (some of these packages are also listed at the previous URL I gave, under the "cluster computing" header).
What speed-up you can get for any number of available cores or computers ultimately depends on the nature of your problems -- and, if the problems per se are suitable for it, then also of the algorithms and data structures you're using... not all will speed-up well (it varies between "embarassingly parallel" problems such as ray-tracing, which speed up linearly all the way, to "intrinsically serial" ones where 100 machines won't be any faster than one). So, it's hard to advise you further without understanding the nature of your problems; care to explain that?
This is not a direct answer to your question but:
Have you considered using Python's multiprocessing module instead? It works by forking new processes, which has slightly more overhead but can often be faster because it avoid contention problems with Python's global interpreter lock. The documentation is quite thorough and there are a number of other articles about it online.
Here is a good tutorial. Section 3.1.2 (page 72 of the tutorial) has a simple client/server example using threads.
the C (CPython) implementation of Python is multi-threaded but NOT concurrent. Only one thread runs at a time, because of the Global Interpeter Lock (GIL). If you want true concurrency you can use the mulitprocessing module.
None of the examples posted will help your multi-hour process run shorter, they will actually cause it to run L O N G E R.
Also you don't mention what you are actually doing, but you are probably I/O bound if you are reading/writing data to anything (network or disk). And concurrency will just exacerbate the problem if that is the case.
Example 1
import thread
class sync:
def __init__(self, threads):
self.__threads = threads
self.__count = 0
self.__main = thread.allocate_lock()
self.__exit = thread.allocate_lock()
self.__exit.acquire()
def sync(self):
self.__main.acquire()
self.__count += 1
if self.__count < self.__threads:
self.__main.release()
else:
self.__exit.release()
self.__exit.acquire()
self.__count -= 1
if self.__count > 0:
self.__exit.release()
else:
self.__main.release()
def example():
def get_input(share):
while share[0]:
share[1] = raw_input('Please say something.\n')
share[2].sync()
share[3].sync()
def do_output(share):
while share[0]:
share[2].sync()
print 'You said, "%s"' % share[1]
share[3].sync()
share = [True, None, sync(2), sync(3)]
thread.start_new_thread(get_input, (share,))
thread.start_new_thread(do_output, (share,))
import time; time.sleep(60)
share[0] = False
share[3].sync()
if __name__ == '__main__':
example()
Example 2
from os.path import basename
from Queue import Queue
from random import random
from sys import argv, exit
from threading import Thread
from time import sleep
# for creating widgets
class Widget:
pass
# for creating stacks
class Stack:
def __init__(self):
self.__stack = list()
def __len__(self):
return len(self.__stack)
def push(self, item):
self.__stack.append(item)
def pop(self):
return self.__stack.pop()
# provides an outline for the execution of the program
def main():
# check and parse the command line arguments
parse_sys_argv()
# setup the variables used by the threads
run_flag = [True]
queue = Queue(argv[1])
send = Stack()
recv = Stack()
# start the threads
producer = Thread(target=produce, args=(run_flag, queue, send))
consumer = Thread(target=consume, args=(run_flag, queue, recv, producer))
producer.start()
consumer.start()
# let the threads do their work
sleep(argv[2])
run_flag[0] = False
consumer.join()
# verify that the solution was valid
calculate_results(send, recv)
# parses and checks the command line arguments
def parse_sys_argv():
try:
# there should be two command line arguments
assert len(argv) == 3
# convert <buf_size> and check
argv[1] = abs(int(argv[1]))
assert argv[1] > 0
# convert <run_time> and check
argv[2] = abs(float(argv[2]))
assert argv[2] > 0
except:
# print out usage information
print basename(argv[0]),
print '<buf_size> <run_time>'
# exits the program
exit(1)
# called by the producer thread
def produce(run_flag, queue, send):
while run_flag[0]:
# simulate production
sleep(random())
# put widget in buffer
item = Widget()
queue.put(item)
send.push(item)
# called by the consumer thread
def consume(run_flag, queue, recv, producer):
# consume items while running
while run_flag[0]:
do_consume(queue, recv)
# empty the queue to allow maximum room
while not queue.empty():
do_consume(queue, recv)
# wait for the producer to end
producer.join()
# consume any other items that might have been produced
while not queue.empty():
do_consume(queue, recv)
# executes one consumption operation
def do_consume(queue, recv):
# get a widget from the queue
recv.push(queue.get())
# simulate consumption
sleep(random())
# verifies that send and recv were equal
def calculate_results(send, recv):
print 'Solution has',
try:
# make sure that send and recv have the same length
assert len(send) == len(recv)
# check all of the contents of send and recv
while send:
# check the identity of the items in send and recv
assert send.pop() is recv.pop()
print 'passed.'
except:
print 'failed.'
# starts the program
if __name__ == '__main__':
main()
Example 3
from os.path import basename
from Queue import Queue
from random import random, seed
from sys import argv, exit
from threading import Thread
from time import sleep
################################################################################
class Widget:
pass
class Stack:
def __init__(self):
self.__stack = list()
def __len__(self):
return len(self.__stack)
def push(self, item):
self.__stack.append(item)
def pop(self):
return self.__stack.pop()
################################################################################
def main():
parse_argv()
run_flag, buffer_queue, producer_stack, consumer_stack, print_queue = [True], Queue(argv[1]), Stack(), Stack(), Queue()
producer_thread = Thread(target=producer, args=(run_flag, argv[3], buffer_queue, producer_stack, print_queue))
consumer_thread = Thread(target=consumer, args=(run_flag, producer_thread, buffer_queue, consumer_stack, argv[4], print_queue))
printer_thread = Thread(target=printer, args=(run_flag, consumer_thread, print_queue))
producer_thread.start()
consumer_thread.start()
printer_thread.start()
sleep(argv[2])
run_flag[0] = False
printer_thread.join()
check_results(producer_stack , consumer_stack)
def parse_argv():
try:
assert len(argv) > 4
argv[1] = abs(int(argv[1]))
argv[2] = abs(float(argv[2]))
assert argv[1] and argv[2]
argv[3] = abs(float(argv[3]))
argv[4] = abs(float(argv[4]))
if len(argv) > 5:
seed(convert(' '.join(argv[5:])))
except:
print basename(argv[0]), '<buff_size> <main_time> <prod_time> <cons_time> [<seed>]'
exit(1)
def convert(string):
number = 1
for character in string:
number <<= 8
number += ord(character)
return number
def check_results(producer_stack , consumer_stack):
print 'Solution has',
try:
assert len(producer_stack) == len(consumer_stack)
while producer_stack:
assert producer_stack.pop() is consumer_stack.pop()
print 'passed.'
except:
print 'failed.'
################################################################################
def producer(run_flag, max_time, buffer_queue, producer_stack, print_queue):
while run_flag[0]:
sleep(random() * max_time)
widget = Widget()
buffer_queue.put(widget)
producer_stack.push(widget)
print_queue.put('Producer: %s Widget' % id(widget))
def consumer(run_flag, producer_thread, buffer_queue, consumer_stack, max_time, print_queue):
while run_flag[0] or producer_thread.isAlive() or not buffer_queue.empty():
widget = buffer_queue.get()
consumer_stack.push(widget)
sleep(random() * max_time)
print_queue.put('Consumer: %s Widget' % id(widget))
def printer(run_flag, consumer_thread, print_queue):
while run_flag[0] or consumer_thread.isAlive() or not print_queue.empty():
if print_queue.empty():
sleep(0.1)
else:
print print_queue.get()
################################################################################
if __name__ == '__main__':
main()
Example 4
import socket
import sys
import thread
def main(setup, error):
sys.stderr = file(error, 'a')
for settings in parse(setup):
thread.start_new_thread(server, settings)
lock = thread.allocate_lock()
lock.acquire()
lock.acquire()
def parse(setup):
settings = list()
for line in file(setup):
parts = line.split()
settings.append((parts[0], int(parts[1]), int(parts[2])))
return settings
def server(*settings):
try:
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dock_socket.bind(('', settings[2]))
dock_socket.listen(5)
while True:
client_socket = dock_socket.accept()[0]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((settings[0], settings[1]))
thread.start_new_thread(forward, (client_socket, server_socket))
thread.start_new_thread(forward, (server_socket, client_socket))
finally:
thread.start_new_thread(server, settings)
def forward(source, destination):
string = ' '
while string:
string = source.recv(1024)
if string:
destination.sendall(string)
else:
source.shutdown(socket.SHUT_RD)
destination.shutdown(socket.SHUT_WR)
if __name__ == '__main__':
main('proxy.ini', 'error.log')
Example 5
# #include <windows.h>
import thread
# #include <math.h>
import math
# #include <stdio.h>
import sys
# #include <stdlib.h>
import time
# static int runFlag = TRUE;
runFlag = True
# void main(int argc, char *argv[]) {
def main(argc, argv):
global runFlag
# unsigned int runTime
# PYTHON: NO CODE
# SYSTEMTIME now;
# PYTHON: NO CODE
# WORD stopTimeMinute, stopTimeSecond;
# PYTHON: NO CODE
# // Get command line argument, N
try:
N = abs(int(argv[1]))
except:
sys.exit(1)
# // Get the time the threads should run, runtime
try:
runTime = abs(int(argv[2]))
except:
sys.exit(1)
# // Calculate time to halt (learn better ways to do this later)
# GetSystemTime(&now);
now = time.localtime()
# printf("mthread: Suite starting at system time
# %d:%d:%d\n", now.wHour, now.wMinute, now.wSecond);
sys.stdout.write('mthread: Suite starting at system time %d:%d:%d\n' \
% (now.tm_hour, now.tm_min, now.tm_sec))
# stopTimeSecond = (now.wSecond + (WORD) runTime) % 60;
stopTimeSecond = (now.tm_sec + runTime) % 60
# stopTimeMinute = now.wMinute + (now.wSecond +
# (WORD) runTime) / 60;
stopTimeMinute = now.tm_min + (now.tm_sec + runTime) / 60
# // For 1 to N
# for (i = 0; i < N; i++) {
for i in range(N):
# // Create a new thread to execute simulated word
thread.start_new_thread(threadWork, ())
# Sleep(100); // Let newly created thread run
time.sleep(0.1)
# }
# PYTHON: NO CODE
# // Cycle while children work ...
# while (runFlag) {
while runFlag:
# GetSystemTime(&now);
now = time.localtime()
# if ((now.wMinute >= stopTimeMinute)
# &&
# (now.wSecond >= stopTimeSecond)
# )
if now.tm_min >= stopTimeMinute \
and now.tm_sec >= stopTimeSecond:
# runFlag = FALSE;
runFlag = False
# Sleep(1000);
time.sleep(1)
# }
# PYTHON: NO CODE
# Sleep(5000);
time.sleep(5)
# }
# PYTHON: NO CODE
# // The code executed by each worker thread (simulated work)
# DWORD WINAPI threadWork(LPVOID threadNo) {
def threadWork():
threadNo = thread.get_ident()
# // Local variables
# double y;
# PYTHON: NO CODE
# const double x = 3.14159;
x = 3.14159
# const double e = 2.7183;
e = 2.7183
# int i;
# PYTHON: NO CODE
# const int napTime = 1000; // in milliseconds
napTime = 1000
# const int busyTime = 40000;
busyTime = 40000
# DWORD result = 0;
result = 0
# // Create load
# while (runFlag) {
while runFlag:
# // Parameterized processor burst phase
# for (i = 0; i < busyTime; i++)
for i in range(busyTime):
# y = pow(x, e);
y = math.pow(x, e)
# // Parameterized sleep phase
# Sleep(napTime);
time.sleep(napTime / 1000.0)
# // Write message to stdout
sys.stdout.write('Thread %s just woke up.\n' % threadNo)
# }
# PYTHON: NO CODE
# // Terminating
# return result;
return result
# }
# PYTHON: NO CODE
if __name__ == '__main__':
main(len(sys.argv), sys.argv)
Example 6
import tkinter
import _thread
import time
EPOCH_DELTA = 946684800
MICREV_IN_DAY = 1000000
MILREV_IN_DAY = 1000
SECOND_IN_DAY = 86400
DAY_IN_WEEK = 7
WEEK_IN_MONTH = 4
MONTH_IN_SEASON = 3
SEASON_IN_YEAR = 4
SECOND_IN_WEEK = SECOND_IN_DAY * DAY_IN_WEEK
SECOND_IN_MONTH = SECOND_IN_WEEK * WEEK_IN_MONTH
SECOND_IN_SEASON = SECOND_IN_MONTH * MONTH_IN_SEASON
SECOND_IN_YEAR = SECOND_IN_SEASON * SEASON_IN_YEAR
def seconds():
"Return seconds since the epoch."
return time.time() - EPOCH_DELTA
def micrev(seconds):
"Convert from seconds to micrev."
x = seconds % SECOND_IN_DAY * MICREV_IN_DAY / SECOND_IN_DAY % MILREV_IN_DAY
return int(x)
def milrev(seconds):
"Convert from seconds to milrev."
x = seconds % SECOND_IN_DAY * MILREV_IN_DAY / SECOND_IN_DAY
return int(x)
def day(seconds):
"Convert from seconds to days."
x = seconds / SECOND_IN_DAY % DAY_IN_WEEK
return int(x)
def week(seconds):
"Convert from seconds to weeks."
x = seconds / SECOND_IN_WEEK % WEEK_IN_MONTH
return int(x)
def month(seconds):
"Convert from seconds to months."
x = seconds / SECOND_IN_MONTH % MONTH_IN_SEASON
return int(x)
def season(seconds):
"Convert from seconds to seasons."
x = seconds / SECOND_IN_SEASON % SEASON_IN_YEAR
return int(x)
def year(seconds):
"Convert from seconds to years."
x = seconds / SECOND_IN_YEAR
return int(x)
UNITS = year, season, month, week, day, milrev, micrev
def text(seconds, spec='{0}.{1}.{2}.{3}.{4}.{5:03}.{6:03}', unit=UNITS):
"Convert from seconds to text."
return spec.format(*[func(seconds) for func in unit])
class Quantum_Timer:
"Quantum_Timer(function, *args, **kwargs) -> Quantum_Timer"
def __init__(self, function, *args, **kwargs):
"Initialize the Quantum_Timer object."
self.__function = function
self.__args = args
self.__kwargs = kwargs
self.__thread = False
self.__lock = _thread.allocate_lock()
def start(self):
"Start the Quantum_Timer object."
with self.__lock:
self.__active = True
if not self.__thread:
self.__thread = True
_thread.start_new_thread(self.__run, ())
def stop(self):
"Stop the Quantum_Timer object."
with self.__lock:
self.__active = False
def __run(self):
"Private class method."
while True:
secs = time.clock()
plus = secs + 0.0864
over = plus % 0.0864
diff = plus - secs - over
time.sleep(diff)
with self.__lock:
if not self.__active:
self.__thread = False
break
self.__function(*self.__args, **self.__kwargs)
def main():
root = tkinter.Tk()
root.resizable(False, False)
root.title('Time in Tessaressunago')
secs = tkinter.StringVar()
text = tkinter.Label(textvariable=secs, font=('helvetica', 16, 'bold'))
text.grid(padx=5, pady=5)
thread = Quantum_Timer(update, secs)
thread.start()
root.mainloop()
def update(secs):
s = seconds()
t = text(s)
p = 1000000000 * 1.01 ** (s / SECOND_IN_YEAR)
secs.set('Time = {0}\nNational = {1}'.format(t, fix(p)))
def fix(number, sep=','):
number = str(int(number))
string = ''
while number:
string = number[-1] + string
number = number[:-1]
if number and not (len(string) + 1) % 4:
string = sep + string
return string
if __name__ == '__main__':
main()
Example 7
HOST = '127.0.0.1'
PORT = 8080
from Tkinter import *
import tkColorChooser
import socket
import thread
import cPickle
################################################################################
class ZSP:
'ZSP(socket) -> ZSP'
def __init__(self, socket):
'Initialize the Zero SPOTS Protocol object.'
self.__file = socket.makefile('b', 0)
def send(self, obj):
'Send one object.'
cPickle.dump(obj, self.__file, cPickle.HIGHEST_PROTOCOL)
def recv(self):
'Receive one object.'
return cPickle.load(self.__file)
################################################################################
def main():
global hold, fill, draw, look
hold = []
fill = '#000000'
connect()
root = Tk()
root.title('Paint 2.0')
root.resizable(False, False)
upper = LabelFrame(root, text='Your Canvas')
lower = LabelFrame(root, text='Their Canvas')
draw = Canvas(upper, bg='#ffffff', width=400, height=300, highlightthickness=0)
look = Canvas(lower, bg='#ffffff', width=400, height=300, highlightthickness=0)
cursor = Button(upper, text='Cursor Color', command=change_cursor)
canvas = Button(upper, text='Canvas Color', command=change_canvas)
draw.bind('<Motion>', motion)
draw.bind('<ButtonPress-1>', press)
draw.bind('<ButtonRelease-1>', release)
draw.bind('<Button-3>', delete)
upper.grid(padx=5, pady=5)
lower.grid(padx=5, pady=5)
draw.grid(row=0, column=0, padx=5, pady=5, columnspan=2)
look.grid(padx=5, pady=5)
cursor.grid(row=1, column=0, padx=5, pady=5, sticky=EW)
canvas.grid(row=1, column=1, padx=5, pady=5, sticky=EW)
root.mainloop()
################################################################################
def connect():
try:
start_client()
except:
start_server()
thread.start_new_thread(processor, ())
def start_client():
global ZSP
server = socket.socket()
server.connect((HOST, PORT))
ZSP = ZSP(server)
def start_server():
global ZSP
server = socket.socket()
server.bind(('', PORT))
server.listen(1)
ZSP = ZSP(server.accept()[0])
def processor():
while True:
func, args, kwargs = ZSP.recv()
getattr(look, func)(*args, **kwargs)
def call(func, *args, **kwargs):
ZSP.send((func, args, kwargs))
################################################################################
def change_cursor():
global fill
color = tkColorChooser.askcolor(color=fill)[1]
if color is not None:
fill = color
def change_canvas():
color = tkColorChooser.askcolor(color=draw['bg'])[1]
if color is not None:
draw.config(bg=color)
call('config', bg=color)
################################################################################
def motion(event):
if hold:
hold.extend([event.x, event.y])
event.widget.create_line(hold[-4:], fill=fill, tag='TEMP')
call('create_line', hold[-4:], fill=fill, tag='TEMP')
def press(event):
global hold
hold = [event.x, event.y]
def release(event):
global hold
if len(hold) > 2:
event.widget.delete('TEMP')
event.widget.create_line(hold, fill=fill, smooth=True)
call('delete', 'TEMP')
call('create_line', hold, fill=fill, smooth=True)
hold = []
def delete(event):
event.widget.delete(ALL)
call('delete', ALL)
################################################################################
if __name__ == '__main__':
main()
Example 8
HOST = '127.0.0.1'
PORT = 8080
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import tkColorChooser
except ImportError:
import tkinter.colorchooser as tkColorChooser
try:
import thread
except ImportError:
import _thread as thread
import socket
import pickle
import time
import sys
################################################################################
class ZSP:
'ZSP(socket) -> ZSP'
def __init__(self, socket):
'Initialize the Zero SPOTS Protocol object.'
self.__o_file = socket.makefile('bw', 0)
self.__i_file = socket.makefile('br', 0)
def send(self, obj):
'Send one object.'
pickle.dump(obj, self.__o_file, pickle.HIGHEST_PROTOCOL)
def recv(self):
'Receive one object.'
return pickle.load(self.__i_file)
################################################################################
class QRP:
'QRP(ZSP) -> QRP'
def __init__(self, ZSP):
'Initialize the Query/Reply Protocol object.'
self.__ZSP = ZSP
self.__error = None
self.__Q_anchor = []
self.__Q_packet = []
self.__R_anchor = {}
self.__Q_lock = thread.allocate_lock()
self.__R_lock = thread.allocate_lock()
thread.start_new_thread(self.__thread, ())
def send_Q(self, ID, obj):
'Send one query.'
if self.__error:
raise self.__error
self.__ZSP.send((False, ID, obj))
def recv_Q(self, timeout=None):
'Receive one query.'
if self.__error:
raise self.__error
if timeout is not None:
if not isinstance(timeout, (float, int)):
raise TypeError('timeout must be of type float or int')
if not timeout >= 0:
raise ValueError('timeout must be greater than or equal to 0')
self.__Q_lock.acquire()
try:
try:
if self.__Q_packet:
Q = True
ID, obj = self.__Q_packet.pop()
else:
Q = False
anchor = [thread.allocate_lock()]
anchor[0].acquire()
self.__Q_anchor.append(anchor)
finally:
self.__Q_lock.release()
except AttributeError:
raise self.__error
if Q:
return ID, obj
if timeout:
thread.start_new_thread(self.__Q_thread, (timeout, anchor))
anchor[0].acquire()
try:
Q = anchor[1]
except IndexError:
if self.__error:
raise self.__error
raise Warning
return Q
def send_R(self, ID, obj):
'Send one reply.'
if self.__error:
raise self.__error
self.__ZSP.send((True, ID, obj))
def recv_R(self, ID, timeout=None):
'Receive one reply.'
if self.__error:
raise self.__error
if timeout is not None:
if not isinstance(timeout, (float, int)):
raise TypeError('timeout must be of type float or int')
if not timeout >= 0:
raise ValueError('timeout must be greater than or equal to 0')
anchor = [thread.allocate_lock()]
anchor[0].acquire()
self.__R_lock.acquire()
try:
try:
self.__R_anchor[ID] = anchor
finally:
self.__R_lock.release()
except AttributeError:
raise self.__error
if timeout:
thread.start_new_thread(self.__R_thread, (timeout, ID))
anchor[0].acquire()
try:
R = anchor[1]
except IndexError:
if self.__error:
raise self.__error
raise Warning
return R
def __thread(self):
'Private class method.'
try:
while True:
R, ID, obj = self.__ZSP.recv()
if R:
self.__R_lock.acquire()
if self.__R_anchor:
self.__R_anchor[ID].append(obj)
self.__R_anchor[ID][0].release()
del self.__R_anchor[ID]
self.__R_lock.release()
else:
self.__Q_lock.acquire()
if self.__Q_anchor:
anchor = self.__Q_anchor.pop()
anchor.append((ID, obj))
anchor[0].release()
else:
self.__Q_packet.append((ID, obj))
self.__Q_lock.release()
except Exception:
error = sys.exc_info()[1]
if isinstance(error, EOFError):
self.__error = EOFError
else:
self.__error = IOError
self.__Q_lock.acquire()
for anchor in self.__Q_anchor:
anchor[0].release()
del self.__Q_anchor
del self.__Q_packet
self.__Q_lock.release()
self.__R_lock.acquire()
for key in self.__R_anchor:
self.__R_anchor[key][0].release()
del self.__R_anchor
self.__R_lock.release()
def __Q_thread(self, timeout, anchor):
'Private class method.'
time.sleep(timeout)
self.__Q_lock.acquire()
if not self.__error and anchor in self.__Q_anchor:
anchor[0].release()
self.__Q_anchor.remove(anchor)
self.__Q_lock.release()
def __R_thread(self, timeout, ID):
'Private class method.'
time.sleep(timeout)
self.__R_lock.acquire()
if not self.__error and ID in self.__R_anchor:
self.__R_anchor[ID][0].release()
del self.__R_anchor[ID]
self.__R_lock.release()
################################################################################
class QRI:
'QRI(QRP) -> QRI'
def __init__(self, QRP):
'Initialize the Query/Reply Interface object.'
self.__QRP = QRP
self.__ID = 0
self.__lock = thread.allocate_lock()
def call(self, obj, timeout=None):
'Send one query and receive one reply.'
self.__lock.acquire()
ID = ''.join(chr(self.__ID >> shift & 0xFF) for shift in range(24, -8, -8))
self.__ID = (self.__ID + 1) % (2 ** 32)
self.__lock.release()
self.__QRP.send_Q(ID, obj)
return self.__QRP.recv_R(ID, timeout)
def query(self, timeout=None):
'Receive one query.'
return self.__QRP.recv_Q(timeout)
def reply(self, ID, obj):
'Send one reply.'
self.__QRP.send_R(ID, obj)
################################################################################
def qri(socket):
'Construct a QRI object.'
return QRI(QRP(ZSP(socket)))
################################################################################
def main():
global hold, fill, draw, look
hold = []
fill = '#000000'
connect()
root = Tk()
root.title('Paint 1.0')
root.resizable(False, False)
upper = LabelFrame(root, text='Your Canvas')
lower = LabelFrame(root, text='Their Canvas')
draw = Canvas(upper, bg='#ffffff', width=400, height=300, highlightthickness=0)
look = Canvas(lower, bg='#ffffff', width=400, height=300, highlightthickness=0)
cursor = Button(upper, text='Cursor Color', command=change_cursor)
canvas = Button(upper, text='Canvas Color', command=change_canvas)
draw.bind('<Motion>', motion)
draw.bind('<ButtonPress-1>', press)
draw.bind('<ButtonRelease-1>', release)
draw.bind('<Button-3>', delete)
upper.grid(padx=5, pady=5)
lower.grid(padx=5, pady=5)
draw.grid(row=0, column=0, padx=5, pady=5, columnspan=2)
look.grid(padx=5, pady=5)
cursor.grid(row=1, column=0, padx=5, pady=5, sticky=EW)
canvas.grid(row=1, column=1, padx=5, pady=5, sticky=EW)
root.mainloop()
################################################################################
def connect():
try:
start_client()
except:
start_server()
thread.start_new_thread(processor, ())
def start_client():
global QRI
server = socket.socket()
server.connect((HOST, PORT))
QRI = qri(server)
def start_server():
global QRI
server = socket.socket()
server.bind(('', PORT))
server.listen(1)
QRI = qri(server.accept()[0])
def processor():
while True:
ID, (func, args, kwargs) = QRI.query()
getattr(look, func)(*args, **kwargs)
def call(func, *args, **kwargs):
try:
QRI.call((func, args, kwargs), 0.05)
except:
pass
################################################################################
def change_cursor():
global fill
color = tkColorChooser.askcolor(color=fill)[1]
if color is not None:
fill = color
def change_canvas():
color = tkColorChooser.askcolor(color=draw['bg'])[1]
if color is not None:
draw['bg'] = color
draw.config(bg=color)
call('config', bg=color)
################################################################################
def motion(event):
if hold:
hold.extend([event.x, event.y])
event.widget.create_line(hold[-4:], fill=fill, tag='TEMP')
call('create_line', hold[-4:], fill=fill, tag='TEMP')
def press(event):
global hold
hold = [event.x, event.y]
def release(event):
global hold
if len(hold) > 2:
event.widget.delete('TEMP')
event.widget.create_line(hold, fill=fill, smooth=True)
call('delete', 'TEMP')
call('create_line', hold, fill=fill, smooth=True)
hold = []
def delete(event):
event.widget.delete(ALL)
call('delete', ALL)
################################################################################
if __name__ == '__main__':
main()

Categories