Good evening all,
I have created a CPU heavy function which I need to iterate 50 times with input from a list and then I am using the multiprocessing pool to synchronously compute the 50 functions. It works but creates a new window for each new process and only after I close these windows I do get the result. Is there a way to not open the window each time? I have tried multiple multiprocessing methods, but all do the same.
import sqlite3
import random
import time
import concurrent.futures
from multiprocessing import Pool
from tkinter import *
import time
window = Tk()
window.title("APP")
def bigF(list):
n=list[0] # Extract multiple var from list
n2=list[1]
new = f(n) # The CPU heavy computation.
if n2 == new:
return True
else:
return False
def Screen1():
window.geometry("350x250")
# Lables and entry windows.
btn = Button(window, text='S2', command=Screen2)
btn.pack(pady=10)
def Screen2():
window.geometry("350x220")
# Lables and entry windows.
def getP():
user = user.get()
cursor.execute(database)
try:
return cursor.fetchall()[0][0]
except IndexError:
raise ValueError('')
def getS():
user = user.get()
cursor.execute(database)
return cursor.fetchall()[0][0]
def getS2():
user = user.get()
cursor.execute(database)
return cursor.fetchall()[0][0]
def function():
try:
var = getP()
except ValueError:
return False
s = getS()
s2 = getS2()
t = t.get()
if __name__ == '__main__':
list1=[]
listt=[]
list3=[]
list1[:0]=somestring
random.shuffle(list1)
for i in range(len(list1)):
listt.append(list1[i])
listt.append(var)
listt.append(s)
listt.append(s2)
listt.append(t)
list3.append(listt)
listt=[]
with Pool(50) as p:
values = p.map(bigF, list3)
if True in values:
someF()
else:
# Lables
btn = Button(window, text='function', command=function)
btn.pack(pady=10)
sgn = Button(window, text='S1', command=Screen1)
sgn.pack(padx=10, side=RIGHT)
def someF():
window.geometry("350x350")
# Lables and entry windows.
Screen1()
window.mainloop()
I don't know if the problem is in bigF(list) or the multiprocessing way. I aim to shorten the processing time to less than 2 seconds where 1 bigF(list) takes around 0.5 seconds.
Thank you for any suggestions and comments.
You need to read the cautions in the multiprocessing doc. When your secondary processes start, it starts a new copy of the interpreter and re-executes your main code. That includes starting a new main window. Anything that should be run in the main process only needs to be inside if __name__=='__main__':. So you need:
if __name__ == '__main__':
windows = Tk()
window.title("APP")
Screen1()
window.mainloop()
Related
I want to write code that does something like-
In a thread call a function that will return two links and in the main thread keep printing
"processing..." until that function, called in secondary thread returns those values
and when we get those return values the while loop of the main thread terminates and print
those
values.
Now I have tried writing few codes in python but couldn't manage to do it!
I have just started python programming so I'm not familiar with it.
BTW the above-mentioned scenario is just a prototype.
The real case looks something like that:-
def search_button(self): #main thread
mname = self.root.ids.name.text
quality = Quality
#print(mname)
#print(quality)
global link4, link5
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = executor.submit(movie_bot, mname, quality) #movie_bot is function
link4, link5 = futures.result() #returning two links
while(link4==None and link5==None):
self.root.ids.status.text = 'Searching, please wait...'
self.root.ids.status.text = ''
print(link4)
print(link5)
In case further details are required then please just let me know.
Thank you for the help.
Tried a lot of things and now finally it got resolved.
Basically what I wanted to do was take two inputs from GUI then call a function (from another python program) with those two parameters and then once the processing is completed then from GUI user and either watch or download that content by pressing the watch or download buttons respectively.
So to do that earlier I was returning the watch and download link from that thread called function and even on calling that function on another thread, as it was returnig values after execution so the GUI freezes and shows not responding
Then after trying a lot of thing I came across daemon thing so I just made that thread daemon and that solved the main problem of freezing but now I wasn't able to take return values (when I tried to take the return values it again started to freeze the GUI)
So then I found an alternative to access those links from the main thread.
Here the point is if the function doesn't return anything that is being called in the thread then just make it daemon thread_name.daemon() = True and now it won't freeze the GUI
Now in case you wanna do something exactly after the thread is finished then this can be used thread_name.is_alive()
MY CODE LOOKS SOMETHING LIKE THAT:-
from selenium_l_headless import movie_bot
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.properties import ObjectProperty
from selenium_l_headless import *
import threading
import time
from kivy.clock import Clock
class MovieBot(MDApp):
mname = ObjectProperty(None)
quality = ObjectProperty(None)
Quality = ObjectProperty(None)
link4 = ObjectProperty(None)
link5 = ObjectProperty(None)
checks = []
def build(self):
self.theme_cls.theme_style= "Dark"
self.theme_cls.primary_palette = "Teal"
return Builder.load_file('kivy_bot_md.kv')
def checkBox_click(self, instance, value, Q):
global Quality
if value==True:
MovieBot.checks.append(Q)
Quality=''
for x in MovieBot.checks:
Quality = f'{Quality}{x}'
else:
MovieBot.checks.remove(Q)
Quality = ''
for x in MovieBot.checks:
Quality = f'{Quality} {x}'
def complete(self):
self.root.ids.status.text = 'Searching completed, Now you can
download or watch the movie!'
global flag
flag=0
def status_sleep(self, *args):
try:
self.root.ids.status.text = 'Searching, please wait...'
if(t1.is_alive() is False):
self.complete()
except:
pass
def search_button(self):
try:
mname = self.root.ids.name.text
quality = Quality
global link4,link5
global t1, flag
flag=1
t1 = threading.Thread(target = movie_bot, args= [mname, quality])
t1.daemon = True
t1.start()
if(t1.is_alive()):
Clock.schedule_interval(self.status_sleep,1)
except:
pass
def watch(self):
try:
if(flag is 1):
pass
else:
self.root.ids.status.text = ''
t2 = threading.Thread(target=watch_now)
t2.daemon = True
t2.start()
except:
pass
def download(self):
try:
if(flag is 1):
pass
else:
self.root.ids.status.text = ''
t3 = threading.Thread(target=download_movie)
t3.daemon = True
t3.start()
except:
pass
def close(self):
exit(0)
MovieBot().run()
I want to change label every sec when i press the button in tk:
# --coding:utf-8 -----
from Tkinter import *
import time
import random
def test(a):
begin =time.time()
end =time.time()
while True:
ran = random.random()
after = time.time()
if(after-begin >1):
a.set(str(ran))
print a.get()
begin =after
if(after-end>10):
a.set('over')
break
t = Tk()
a = StringVar()
a.set('0')
b = Label(t,textvariable = a)
b.pack()
Button(t,text ='test',command = lambda x=a:test(a)).pack()
t.mainloop()
My console output is right,but it doesnot effect on windows.WHY?
You can start the test() function in a separate thread, like this:
# --coding:utf-8 -----
from Tkinter import *
import time
import random
import threading
def startThread(a):
threading.Thread(target=test, args=(a,)).start()
def test(a):
begin =time.time()
end =time.time()
while True:
ran = random.random()
after = time.time()
if(after-begin >1):
a.set(str(ran))
print a.get()
begin =after
if(after-end>10):
a.set('over')
break
t = Tk()
a = StringVar()
a.set('0')
b = Label(t,textvariable = a)
b.pack()
Button(t,text ='test',command = lambda x=a:startThread(a)).pack()
t.mainloop()
However, the thread won't end until the end of the 10 seconds, even if you close the window. You'll need to find some code for the while loop to check if the application is still running.
Creating a behavior that must execute for a given time period is a frequent problem; you can tackle it with a dedicated Waiter class; this avoids the use of threads that are poorly supported by tkinter.
The following example opens a window with a label and a button. When the button is pressed, the label will update every seconds for 10 seconds, then stop.
Pressing the button again within 10 seconds has no effect; after that, it restarts the process for 10 more seconds.
import Tkinter as tk # tkinter if python >= 3
import time
import random
class Waiter(object):
def __init__(self, waiting_time):
"""
:param waiting_time: int, in seconds
"""
self.waiting_time = waiting_time
self.expiring_time = time.time() + self.waiting_time
self.waiting = True
# print('waiter started')
def stop(self):
# print('waiter stopping')
self.expiring_time = None
self.waiting = False
def is_waiting(self):
"""returns True while waiting, false otherwise"""
if time.time() > self.expiring_time:
self.stop()
return self.waiting
def atest():
global waiter
if waiter is None:
waiter = Waiter(10)
_atest()
def _atest():
""" equivalent to:
while the waiter is waiting,
change the label every second),
then destroy the waiter
"""
global waiter
if waiter.is_waiting():
a.set(random.random())
# print(time.time())
t.after(1000, _atest)
else:
waiter = None
if __name__ == '__main__':
waiter = None
t = tk.Tk()
a = tk.StringVar()
a.set('0')
tk.Label(t, textvariable=a).pack()
tk.Button(t, text='test', command=atest).pack()
t.mainloop()
Note:
You could make _atest an inner function of atest, but maybe it is easier to understand as it is?
using import Tkinter as tk instead of from Tkinter import * prevents cluttering the namespace, and arguably makes the code clearer.
You should probably consider using python 3.
I need a little script which put the content of the clipboard in a variable and then "do things" (i.e. execute other functions with the variable as parameter). The script has to do it every time the clipboard is modified.
Right now I have:
def get_clipboard():
root = Tk()
root.withdraw()
try:
return root.clipboard_get()
except:
return ""
if __name__ == '__main__':
cb = ""
while True:
cb_new = get_clipboard()
if cb_new == cb or cb_new == "":
continue
cb = cb_new
print(cb) # Here I will call other functions
print("---------------------------------------------")
time.sleep(0.1)
But I have an error after some time: unable to realloc 28675 bytes. I guess it is because of the while loop, but I don't know how to do it differently. I tried to use mainloop, but I don't understand how it work and if it is what I need.
The get_clipboard function creates a tkinter window but never properly destroys it. Doing this in a loop, 10 times per second, accumulates memory until there isn't enough left to create another window and your script crashes.
Change the function to this:
def get_clipboard():
root = Tk()
root.withdraw()
try:
return root.clipboard_get()
except:
return ""
finally:
root.destroy()
The context:
I'm building a Graphical Interface with Qt creator and the "behaviour" file in python. A test version of my GUI is:
The expected behaviour:
I am running 2 different threads which are referred to the same function with different input arguments. With the SELECTOR button I can assign the value of 1 or 2 to a variable (and display it)
The button Start thread enables the correct thread to start (the first time).
The loop should be turned off by the stop button by modifying the global running variable.
This is my code
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, uic
import sys
import threading
import time
import Queue
running = False
first_thread = None
second_thread = None
form_class = uic.loadUiType("simple2.ui")[0]
q = Queue.Queue()
select = 0
def action(string, queue): #function called by threads
global running
while(running):
phrase = string
if queue.qsize() < 10:
queue.put(phrase)
#else:
# print queue.qsize()
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#buttons
self.startButton.clicked.connect(self.start_clicked)
self.stopButton.clicked.connect(self.stop_clicked)
self.selector.clicked.connect(self.sel_click)
#variables
self.first = False
self.second = False
#queue
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_phrase)
self.timer.start(1)
def start_clicked(self): #start button callback
global select
if select > 0:
global running
running = True
print "started"
if (not self.first) & (select == 1):
first_thread.start()
self.first = True
if (not self.second) & (select == 2):
second_thread.start()
self.second = True
self.startButton.setEnabled(False)
self.startButton.setText('Starting...')
def stop_clicked(self): #stop button callback
global running
running = False
print "stopped"
self.startButton.setEnabled(True)
self.startButton.setText('Start Thread')
def sel_click(self): #selector button callback
global select
if select < 2:
select = select + 1
else:
select = 1
self.thread_counter.setText(str(select))
def update_phrase(self): #looping function
global running
if (not q.empty()) & running:
self.startButton.setText('Thread on')
abc = q.get()
print abc
def closeEvent(self, event):
global running
running = False
if __name__ == "__main__":
first_thread = threading.Thread(target=action, args = ("first", q))
second_thread = threading.Thread(target=action, args = ("second", q))
app = QtGui.QApplication(sys.argv)
w = MyWindowClass(None)
w.setWindowTitle('Multiple threads test in python')
w.show()
app.exec_()
For now, each thread should simple print on terminal their arguments ("First" or "Second").
If threads are started for the first time, my code works. But I would like to switch between threads infinite times.
Since threads cannot be stopped, is there a way to "pause" them?
I cannot find a solution, I hope someone will help me also with a piece of code. Thank you in advance
You can use Lock class to do that, a simple example would be:
import threading
lock = threading.Lock()
//here it will be lock
lock.acquire() # will block if lock is already held
...
then in other side do
//this will wake up
lock.release()
you can read more here http://effbot.org/zone/thread-synchronization.htm
I am trying to run 2 loops at the same time using multiprocessing, but they only seem to run sequentially.
When the first loop starts the mainloop() process for tkinter the other loop doesn't start until the GUI window is shut down, then the count loop starts.
I have tried multithreading and multiprocessing with the same result. I need them to run concurrently. Below is a simple example that demonstrates the problem. I'm Using python 2.7.10.
from multiprocessing import Process
from Tkinter import *
import time
count = 0
def counting():
while True:
global count
count = count + 1
print count
time.sleep(1)
class App():
def __init__(self):
self.myGUI = Tk()
self.myGUI.geometry('800x600')
self.labelVar = StringVar()
self.labelVar.set("test")
self.label1 = Label(self.myGUI, textvariable=self.labelVar)
self.label1.grid(row=0, column=0)
app = App()
t1 = Process(target = app.myGUI.mainloop())
t2 = Process(target = counting())
t1.start()
t2.start()
You are calling the functions, and waiting for them to finish, in order to pass their result as the Process target. Pass the functions themselves instead - that is, change this:
t1 = Process(target = app.myGUI.mainloop())
t2 = Process(target = counting())
to this:
t1 = Process(target=app.myGUI.mainloop)
t2 = Process(target=counting)
So that the Process can call those functions (in a subprocess).