PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label thread-safety. Show all posts
Showing posts with label thread-safety. Show all posts

Saturday, October 1, 2022

[FIXED] What is the difference between SynchronizedCollection<T> and the other concurrent collections?

 October 01, 2022     c#, concurrency, concurrent-collections, synchronizedcollection, thread-safety     No comments   

Issue

How does SynchronizedCollection<T> and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection<T> being a class?

SynchronizedCollection<T> and all of the classes in Concurrent Collections provide thread-safe collections. How do I decide when to use one over the other, and why?


Solution

The SynchronizedCollection<T> class was introduced first in .NET 2.0 to provide a thread-safe collection class. It does this via locking so that you essentially have a List<T> where every access is wrapped in a lock statement.

The System.Collections.Concurrent namespace is much newer. It wasn't introduced until .NET 4.0 and it includes a substantially improved and more diverse set of choices. These classes no longer use locks to provide thread safety, which means they should scale better in a situation where multiple threads are accessing their data simultaneously. However, a class implementing the IList<T> interface is notably absent among these options.

So, if you're targeting version 4.0 of the .NET Framework, you should use one of the collections provided by the System.Collections.Concurrent namespace whenever possible. Just as with choosing between the various types of collections provided in the System.Collections.Generic namespace, you'll need to choose the one whose features and characteristics best fit your specific needs.

If you're targeting an older version of the .NET Framework or need a collection class that implements the IList<T> interface, you'll have to opt for the SynchronizedCollection<T> class.

This article on MSDN is also worth a read: When to Use a Thread-Safe Collection



Answered By - Cody Gray
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, September 30, 2022

[FIXED] How to properly call method asynchronous without blocking main thread?

 September 30, 2022     asynchronous, concurrency, python, thread-safety     No comments   

Issue

I have a service with some kind of main-loop. In this service I need to send request to other service with some metadata about operation. I need to do it asynchronous without blocking main thread.

Here is my code:

def process_response(self, request, response, spider):
    if self.enabled:
        Thread(target=self._send_request_to_information_supplier,
               args=(response,)).start()
        # I don't want this flow to be blocked by above thread.
        return response
    return response


def _send_request_to_information_supplier(self, html_response):
    some_metadata = InformationSupplierMetadata(url=html_response.url,
                                                html=html_response.text).convert_to_dict()
    try:
        request_response = requests.post(url=self.url,
                                         data=some_metadata,
                                         headers={'Content-Type': 'application/json; charset=utf-8'},
                                         timeout=self.timeout,
                                         verify=False)
        self.logger.debug('Request was sent and end up with status {0}'.format(request_response.status_code))
    except ConnectTimeout:
        self.logger.debug('Request timed out')

I am beginner in multi-threading and I am not sure this is the correct way to do it. Maybe someone could suggest better approach?


Solution

you can use the ThreadPoolExecutor. Here is an example that may can help.

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(24)

def process_response(self, request, response, spider):
    if self.enabled:
        executor.submit(self._send_request_to_information_supplier, response)

        # I don't want this flow to be blocked by above thread.
        return response
    return response


def _send_request_to_information_supplier(self, html_response):
    ...

this way the executor will having 24 maximum number of threads, and the submit will run the method in one thread if any available.



Answered By - Fran N.J.
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing