python-multithreading

Is extending a Python list (e.g. l += [1]) guaranteed to be thread-safe?

一曲冷凌霜 提交于 2019-12-02 18:11:07
If I have an integer i , it is not safe to do i += 1 on multiple threads: >>> i = 0 >>> def increment_i(): ... global i ... for j in range(1000): i += 1 ... >>> threads = [threading.Thread(target=increment_i) for j in range(10)] >>> for thread in threads: thread.start() ... >>> for thread in threads: thread.join() ... >>> i 4858 # Not 10000 However, if I have a list l , it does seem safe to do l += [1] on multiple threads: >>> l = [] >>> def extend_l(): ... global l ... for j in range(1000): l += [1] ... >>> threads = [threading.Thread(target=extend_l) for j in range(10)] >>> for thread in

Reading a File without block main thead in GUI

一世执手 提交于 2019-12-02 16:57:35
问题 In my first attempt, I tried, failed, learned, and came back with the following attempt. I have a text file with the names parsed with commas that looks like this: Ann Marie,Smith,ams@companyname.com The list could have over 100+ names in it. I left out the code that generates all the other GUI components to focus on loading the combobox and the items. I'm trying to read a text and load the data into a combobox without blocking the main thread. I consulted a textbook and Pythons documentation

HTTP Requests using a range of IP address on python

∥☆過路亽.° 提交于 2019-12-02 13:02:09
I have a VM as a server with IP address 10.91.55.2. I have another VM which acts as a client having IP address in the range 10.91.56.2......10.91.56.10. I want to write a script that will use all these IP address on the client to send HTTP request to the server (10.91.55.2). I have written a script that sends HTTP requests using the Physical IP address alone. Is there any way to send HTTP Requests from a range of IP address. My OS Is linux. Peter Gibson This is not a complete answer, but should provide you with a starting point. You say "I have another VM which acts as a client", so I'll

Cannot close multithreaded Tkinter app on X button

岁酱吖の 提交于 2019-12-02 12:02:13
My app has the following structure: import tkinter as tk from threading import Thread class MyWindow(tk.Frame): ... # constructor, methods etc. def main(): window = MyWindow() Thread(target=window.mainloop).start() ... # repeatedly draw stuff on the window, no event handling, no interaction main() The app runs perfectly, but if I press the X (close) button, it closes the window, but does not stop the process, and sometimes even throws a TclError . What is the right way to write an app like this? How to write it in a thread-safe way or without threads? Main event loop should in main thread, and

Does pydispatcher run the handler function in a background thread?

醉酒当歌 提交于 2019-12-02 10:13:08
Upon looking up event handler modules, I came across pydispatcher, which seemed beginner friendly. My use case for the library is that I want to send a signal if my queue size is over a threshold. The handler function can then start processing and removing items from the queue (and subsequently do a bulk insert into the database). I would like the handler function to run in the background. I am aware that I can simply overwrite the queue.append() method checking for the queue size and calling the handler function asynchronously, but I would like to implement the listener-dispatcher model to

When to call thread.join in a GUI application

夙愿已清 提交于 2019-12-02 07:53:55
问题 import wx import json import queue from collections import namedtuple import threading class MyDialog(wx.Frame): def __init__(self, parent, title): self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX) wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize) self.panel = wx.Panel(self, size=(250, 270)) self.emp_selection = wx.ComboBox(self.panel, -1, pos=(40, 50), size=(200,100)) self.start_read_thread() #code to load other GUI components

How do I lock an entire SQLite connection (locked read + locked write)?

巧了我就是萌 提交于 2019-12-02 07:49:53
I have an sqlite3 db that is being accessed concurrently. I have ClientA that reads the state of some table (Column1 has rows A , B , C ) and needs to update the table with new letters of the alphabet. If ClientB reads the state of the table before ClientA updates the table (say with the new letter D ), then it's possible that both clients could (and in my case do ) write D to the table - such that Column1 becomes A , B , C , D , D . But I need to ensure Column1 only has unique letters! How do I lock the db connection so that its read AND write operations get exclusive access so that Column1

call function of main thread from secondary thread

笑着哭i 提交于 2019-12-02 07:49:02
问题 I am making a GUI in PyQt for user to create backup of huge data. The GUI ( main thread ) is taking inputs from user. rsync command ( for backup ) is also being called in main thread hence the window is freezing. Aim is to try qthread such that app runs without freezing. My search material : 1 : https://www.youtube.com/watch?v=o81Q3oyz6rg. This video shows how to not freeze GUI by running other task in secondary thread. I've tried it and it works. But it does not help in running the command

Performance issue in python with nested loop

守給你的承諾、 提交于 2019-12-02 07:44:21
I was able to improve a code written in python a lot with numpy because of the dot product. Now I still have one part of the code which is still very slow. I still don't understand multithreading and if this could help here. In my opinion this should be possible here. Do you have a nice idea what to do here? for x1 in range(a**l): for x2 in range(a**l): for x3 in range(a**l): f11 = 0 cv1 = numpy.ndarray.sum( numpy.absolute(numpy.subtract(ws[x1], ws[x2]))) cv2 = numpy.ndarray.sum( numpy.absolute(numpy.subtract(ws[x1], ws[x3]))) if cv1 == 0: f11 += 1 if cv2 == 0: f11 += 1 re[x1][x2][x3] = 1.0*r/

running script multiple times simultaniously in python 2.7

爱⌒轻易说出口 提交于 2019-12-02 06:48:55
问题 Hello I am trying to run a script multiple times but would like this to take place at the same time from what I understood i was to use subprocess and threading together however when i run it it still looks like it is being executed sequentially can someone help me so that i can get it to run the same script over and over but at the same time? is it in fact working and just really slow? edit forgot the last piece of code now at the bottom here is what i have so far import os import datetime