How to achieve multi threading using Scheduler class in Python 3? - multithreading

I want the parallel execution of the task using Scheduler class in Python 3. Please help me with this.
Here is my code.
import sched, time
s = sched.scheduler(time.time)
def print_hello():
print('Hello')
def print_some_times():
s.enter(1, 1,print_hello)
s.run()
print_some_times()
print_some_times()
When I run this code it is showing error.
"RecursionError: maximum recursion depth exceeded while calling a Python object".
Also I want to run another task using above code.

Related

How to use Release and Acquire in Lock in python multithreading?

I was trying to implement a program that can simultaneously change the elements of an array and print it, using multithreading in python.
from threading import Thread
import threading
import random
def print_ele(array):
count=True
while count:
print(array)
def change_ele():
array=[1,2,3,4]
t1=Thread(target=print_ele,args=(array,))
t1.start()
lock=threading.Lock()
random.seed(10)
count=True
while count:
lock.acquire()
for i in range(5):
array[i]=random.random()
lock.release()
change_ele()
I expect to get different random numbers printed in each iteration. But instead it seems that the array gets updated only once.
I know that we can do the same thing without multithreading. But I was wondering if we could do the same thing using multithreading.

Need help for automatization of a Python function

I have here a python program written for an Enigma 2 Linux Set top box:
VirtualZap Python program for Enigma 2 based set top boxes
I want to automatize the execution of the following function every minute:
def aktualisieren(self):
self.updateInfos()
You can find the defined function within line 436 and 437.
My problem is that class VirtualZap contains only one constructor but no main method with the actual program run, therefore it is difficult to implement threads or coroutines. Is there any possibility to automatize the execution of the aktualisieren function?
There is an Advanced Python Scheduler
from apscheduler.schedulers.blocking import BlockingScheduler
def aktualisieren(self):
self.updateInfos()
scheduler = BlockingScheduler()
scheduler.add_job(aktualisieren, 'interval', hours=1)
scheduler.start()

Why does some widgets don't update on Qt5?

I am trying to create a PyQt5 application, where I have used certain labels for displaying status variables. To update them, I have implemented custom pyqtSignal manually. However, on debugging I find that the value of GUI QLabel have changed but the values don't get reflected on the main window.
Some answers suggested calling QApplication().processEvents() occasionally. However, this instantaneously crashes the application and also freezes the application.
Here's a sample code (all required libraries are imported, it's just the part creating problem, the actual code is huge):
from multiprocessing import Process
def sub(signal):
i = 0
while (True):
if (i % 5 == 0):
signal.update(i)
class CustomSignal(QObject):
signal = pyqtSignal(int)
def update(value):
self.signal.emit(value)
class MainApp(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel("0");
self.customSignal = CustomSignal()
self.subp = Process(target=sub, args=(customSignal,))
self.subp.start()
self.customSignal.signal.connect(self.updateValue)
def updateValue(self, value):
print("old value", self.label.text())
self.label.setText(str(value))
print("new value", self.label.text())
The output of the print statements is as expected. However, the text in label does not change.
The update function in CustomSignal is called by some thread.
I've applied the same method to update progress bar which works fine.
Is there any other fix for this, other than processEvents()?
The OS is Ubuntu 16.04.
The key problem lies in the very concept behind the code.
Processes have their own address space, and don't share data with another processes, unless some inter-process communication algorithm is used. Perhaps, multithreading module was used instead of threading module to bring concurrency to avoid Python's GIL and speedup the program. However, subprocess has cannot access the data of parent process.
I have tested two solutions to this case, and they seem to work.
threading module: No matter threading in Python is inefficient due to GIL, but it's still sufficient to some extent for basic concurrency demands. Note the difference between concurrency and speedup.
QThread: Since you are using PyQt, there's isn't any issue in using QThread, which is a better option because it takes concurrency to multiple cores taking advantage of operating system's system call, rather than Python in the middle.
Try adding
self.label.repaint()
immediately after updating the text, like this:
self.label.setText(str(value))
self.label.repaint()

Locking a method in Python?

Here is my problem: I'm using APScheduler library to add scheduled jobs in my application. I have multiple jobs executing same code at the same time, but with different parameters. The problem occurs when these jobs access the same method at the same time which causes my program to work incorrectly.
I wanna know if there is a way to lock a method in Python 3.4 so that only one thread may access it at a time? If so, could you please post a simple example code? Thanks.
You can use a basic python locking mechanism:
from threading import Lock
lock = Lock()
...
def foo():
lock.acquire()
try:
# only one thread can execute code there
finally:
lock.release() #release lock
Or with context managment:
def foo():
with lock:
# only one thread can execute code there
For more details see Python 3 Lock Objects and Thread Synchronization Mechanisms in Python.

What is mean when it raises a PicklingError?

Hy there!
I'm new on python 3.
I'm using the pvmomi module to get a dict of vm's from my server. When i try to run my file, with multiprocessing, i get the following Error:
_pickle.PicklingError: Can't pickle : attribute lookup vim.VirtualMachine on pyVmomi.VmomiSupport failed
What does this mean?
Here is a part of my code:
def login(vm):
#do something
if __name__=='__main__':
cpu = mp.cpu_count()
workers = mp.Pool(cpu)
workers.map(login,range(1))
for vm in vmDict:
login(vm)
My biggest problem comes from the for loop. I need this loop to do the jobs for every dictitem but only one pool worker do the job. Now i have configured my code to this below and it raises the PicklingError.
Thanks for help. It drives me crazy!
The stdlib pickle (.py) module imports the builtin C-coded _pickle module. The pickle module can serialize most Python objects and is used to transport Python objects between processes. In particular, pickle is used by multiprocessing (and perhaps by pyvmomi). User-defined classes sometimes define special methods (reduce and reducex, I believe) to help the pickle and unpickle processes.
The exception message says that an attribute lookup failed. Perhaps the pyVmomi object is not properly configured to be pickled. You might check to module doc to see if it says anything about pickle support.

Resources