python-multithreading

cython shared memory in cython.parallel.prange - block

筅森魡賤 提交于 2019-11-27 14:45:25
I have a function foo that takes a pointer to memory as argument and both writes and reads to that memory: cdef void foo (double *data): data[some_index_int] = some_value_double do_something_dependent_on (data) I'm allocating to data like so: cdef int N = some_int cdef double *data = <double*> malloc (N * sizeof (double)) cdef int i for i in cython.parallel.prange (N, nogil=True): foo (data) readout (data) My question is now: how do the different threads treat this? My guess is that the memory pointed to by data will be shared by all threads and 'simultaneously' read from or written to while

What happened to thread.start_new_thread in python 3

☆樱花仙子☆ 提交于 2019-11-27 13:40:06
问题 I liked the ability to turn a function into a thread without the unnecessary line to define a class. I know about _thread, however it appears that you are not supposed to use _thread. Is there a good-practice equivalent of thread.start_new_thread for python 3? 回答1: threading.Thread(target=some_callable_function).start() or if you wish to pass arguments, threading.Thread(target=some_callable_function, args=(tuple, of, args), kwargs={'dict': 'of', 'keyword': 'args'}, ).start() 回答2:

Python Threading with Event object

偶尔善良 提交于 2019-11-27 13:03:28
问题 I've seen a lot of Python scripts that use Threads in a class and a lot of them use the threading.Event() . For example: class TimerClass(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.event = threading.Event() def run(self): while not self.event.is_set(): print "something" self.event.wait(120) In the while loop, why do they check the condition if they don't set self.event ? 回答1: Because someone else will set it. You generally start a thread in one part of your

Python - appending to same file from multiple threads

一世执手 提交于 2019-11-27 11:57:12
I'm writing an app that appends lines to the same file from multiple threads. I have a problem in which some lines are appended without a new line. Any solution for this? class PathThread(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def printfiles(self, p): for path, dirs, files in os.walk(p): for f in files: print(f, file=output) def run(self): while True: path = self.queue.get() self.printfiles(path) self.queue.task_done() pathqueue = Queue.Queue() paths = getThisFromSomeWhere() output = codecs.open('file', 'a') # spawn threads for i in

python asyncio, how to create and cancel tasks from another thread

孤者浪人 提交于 2019-11-27 10:20:39
问题 I have a python multi-threaded application. I want to run an asyncio loop in a thread and post calbacks and coroutines to it from another thread. Should be easy but I cannot get my head around the asyncio stuff. I came up to the following solution which does half of what I want, feel free to comment on anything: import asyncio from threading import Thread class B(Thread): def __init__(self): Thread.__init__(self) self.loop = None def run(self): self.loop = asyncio.new_event_loop() asyncio.set

Multithreading for Python Django

依然范特西╮ 提交于 2019-11-27 09:36:50
问题 Some functions should run asynchronously on the web server. Sending emails or data post-processing are typical use cases. What is the best (or most pythonic) way write a decorator function to run a function asynchronously? My setup is a common one: Python, Django, Gunicorn or Waitress, AWS EC2 standard Linux For example, here's a start: from threading import Thread def postpone(function): def decorator(*args, **kwargs): t = Thread(target = function, args=args, kwargs=kwargs) t.daemon = True t

Opening a Python thread in a new console window

痴心易碎 提交于 2019-11-27 09:17:42
I am trying to make a program that will launch both a view window (console) and a command line. In the view window, it would show constant updates, while the command line window would use raw_input() to accept commands that affect the view window. I am thinking about using threads for this, but I have no idea how to launch a thread in a new console window. How would I do that? Rather than use a console or terminal window, re-examine your problem. What you are trying to do is create a GUI. There are a number of cross-platform toolkits including Wx and Tkinter that have widgets to do exactly

How to return values from Process- or Thread instances?

流过昼夜 提交于 2019-11-27 08:31:03
问题 So I want to run a function which can either search for information on the web or directly from my own mysql database. The first process will be time-consuming, the second relatively fast. With this in mind I create a process which starts this compound search (find_compound_view). If the process finishes relatively fast it means it's present on the database so I can render the results immediately. Otherwise, I will render "drax_retrieving_data.html". The stupid solution I came up with was to

Pyqt5 qthread + signal not working + gui freeze

我们两清 提交于 2019-11-27 07:20:28
I am trying to make a mailbox checker with imap lib, it work pretty fine with python, queue and multithread without gui. But when I try to put a gui, every fonction i made, make the gui freeze until finish . I tried many thing from various doc(add qthread, signal, cursorr etcc) and tutorials none worked for me . Can someone help me to understand how to set or append a text to a QtextEdit while running a function coz it work only after finish . Here is my code : class Checker(QtCore.QThread): signal = QtCore.pyqtSignal(object) def __init__(self, lignesmailtocheck): QtCore.QThread.__init__(self)

Thread vs. Threading

痴心易碎 提交于 2019-11-27 06:47:28
What's the difference between the threading and thread modules in Python? In Python 3, thread has been renamed to _thread . It is infrastructure code that is used to implement threading , and normal Python code shouldn't be going anywhere near it. _thread exposes a fairly raw view of the underlying OS level processes. This is almost never what you want, hence the rename in Py3k to indicate that it is really just an implementation detail. threading adds some additional automatic accounting, as well as several convenience utilities, all of which makes it the preferred option for standard Python