multithreading

Python Socket Receive/Send Multi-threading

偶尔善良 提交于 2020-05-11 11:52:11
问题 I am writing a Python program where in the main thread I am continuously (in a loop) receiving data through a TCP socket, using the recv function. In a callback function, I am sending data through the same socket, using the sendall function. What triggers the callback is irrelevant. I've set my socket to blocking. My question is, is this safe to do? My understanding is that a callback function is called on a separate thread (not the main thread). Is the Python socket object thread-safe? From

Python Socket Receive/Send Multi-threading

本小妞迷上赌 提交于 2020-05-11 11:52:09
问题 I am writing a Python program where in the main thread I am continuously (in a loop) receiving data through a TCP socket, using the recv function. In a callback function, I am sending data through the same socket, using the sendall function. What triggers the callback is irrelevant. I've set my socket to blocking. My question is, is this safe to do? My understanding is that a callback function is called on a separate thread (not the main thread). Is the Python socket object thread-safe? From

What is the use case for the atomic exchange (read-write) operation?

房东的猫 提交于 2020-05-11 06:29:30
问题 C++0x specifies the std::atomic template for thread safe atomic access to variables. This template has, among others, a member function std::atomic::exchange that atomically stores a new value in "this" and retrieves the existing value of "this". Win32 has a similar function: InterlockedExchange Now, what these operations do is simple: atomic read-modify. What I do not understand is what the point of this operation is. The value read that is returned is "meaningless", because once I can

Flask, Python and Socket.io: multithreading app is giving me “RuntimeError: working outside of request context”

懵懂的女人 提交于 2020-05-11 04:14:07
问题 I have been developing an app using Flask , Python and Flask-Socket.io library. The problem I have is that the following code will not perform an emit correctly due to some contexts issue RuntimeError: working outside of request context I am writing only one python file for the entire program by now. This is my code ( test.py ): from threading import Thread from flask import Flask, render_template, session, request, jsonify, current_app, copy_current_request_context from flask.ext.socketio

Dealing with throttling/rate limits (429 error) when using async/await

浪子不回头ぞ 提交于 2020-05-11 02:55:29
问题 I have the following async code that gets called from so many places in my project: public async Task<HttpResponseMessage> MakeRequestAsync(HttpRequestMessage request) { var client = new HttpClient(); return await client.SendAsync(request).ConfigureAwait(false); } An example of how the above method gets called: var tasks = items.Select(async i => { var response = await MakeRequestAsync(i.Url); //do something with response }); The ZenDesk API that I'm hitting allows about 200 requests per

Dealing with throttling/rate limits (429 error) when using async/await

孤街醉人 提交于 2020-05-11 02:55:17
问题 I have the following async code that gets called from so many places in my project: public async Task<HttpResponseMessage> MakeRequestAsync(HttpRequestMessage request) { var client = new HttpClient(); return await client.SendAsync(request).ConfigureAwait(false); } An example of how the above method gets called: var tasks = items.Select(async i => { var response = await MakeRequestAsync(i.Url); //do something with response }); The ZenDesk API that I'm hitting allows about 200 requests per

Dealing with throttling/rate limits (429 error) when using async/await

人盡茶涼 提交于 2020-05-11 02:55:09
问题 I have the following async code that gets called from so many places in my project: public async Task<HttpResponseMessage> MakeRequestAsync(HttpRequestMessage request) { var client = new HttpClient(); return await client.SendAsync(request).ConfigureAwait(false); } An example of how the above method gets called: var tasks = items.Select(async i => { var response = await MakeRequestAsync(i.Url); //do something with response }); The ZenDesk API that I'm hitting allows about 200 requests per

Python parallel thread that consume Watchdog queue events

北慕城南 提交于 2020-05-09 07:55:06
问题 I have this code that should put an event in a queue each time an external program (TCPdump) creates a *.pcap file in my directory. My problem is that I always get an empty queue, although I got the print from process() function. What am I doing wrong? Is the queue correctly defined and shared between the two classes? EDIT----------------- I maybe understood why I got an empty queue, I think it is because I'm printing the queue that I initialized before it gets filled by Handler class. I

Python : how to stop a thread that's waiting for a .recv()

☆樱花仙子☆ 提交于 2020-05-09 06:20:14
问题 I have this thread running : def run(self): while 1: msg = self.connection.recv(1024).decode() I wish I could end this thread when I close the Tkinter Window like this : self.window.protocol('WM_DELETE_WINDOW', self.closeThreads) def closeThreads(self): self.game.destroy() #End the thread Can't use thread._close() because it is deprecated and python 3.4 does not allow it. 回答1: Two solutions: 1) Don't stop the thread, just allow it to die when the process exits with sys.exit() 2) Start the

Is it possible to access/update the child thread's resource from parent thread?

牧云@^-^@ 提交于 2020-05-08 19:47:07
问题 I'm doing a socket programming in C and I'm completely new to Multithreading. This is my scenario, I need a parent thread which reads the data from socket(lets say..) and enqueue the same to the queue of its child thread.The problem here is, how can I update the queue which is child thread-specific from the parent thread. 回答1: Thread-safe queue that supports multiple producers and consumers. MtQueue.h : #ifndef MtQueue_H #define MtQueue_H #include <pthread.h> #include <stdlib.h> // A fixed