background preloader

Parallel

Facebook Twitter

Python Concurrency: An Example of a Queue. Python comes with a lot of cool concurrency tools builtin, such as threads, Queues, semaphores and multiprocessing. In this article, we’ll spend some time learning how to use Queues. A Queue can be used for first-in-first out or last-in-last-out stack-like implementations if you just use them directly. If you’d like to see that in action, see the Hellman article at the end of this post. We’re going to mix threads in and create a simple file downloader script to demonstrate how Queues work for cases where we want concurrency. Creating a Downloading Application This code is based loosely on Hellman’s article and the IBM article as they both show how to download URLs in various ways. Import osimport Queueimport threadingimport urllib2 ########################################################################class Downloader(threading.Thread): """Threaded File Downloader""" #---------------------------------------------------------------------- def __init__(self, queue): threading.Thread.

How do I Find Out Linux CPU Utilization? Whenever a Linux system CPU is occupied by a process, it is unavailable for processing other requests. Rest of pending requests must wait till CPU is free. This becomes a bottleneck in the system. Following command will help you to identify CPU utilization, so that you can troubleshoot CPU related performance problems. Finding CPU utilization is one of the important tasks. Linux comes with various utilities to report CPU utilization. . * CPU utilization * Display the utilization of each CPU individually (SMP cpu) * Find out your system's average CPU utilization since the last reboot etc * Determine which process is eating the CPU(s) Old good top command to find out Linux cpu load The top program provides a dynamic real-time view of a running system. Top command to find out Linux cpu usage Type the top command: $ top Output: You can see Linux CPU utilization under CPU stats.

The top command produces a frequently-updated list of processes. Find Linux CPU utilization using mpstat and other tools. Multithreaded data structures for parallel computing: Part 2, Designing concurrent data structures without mutexes. Introduction This article—the concluding part in this series—discusses two things: Design choices for implementing a mutex based concurrent list and designing concurrent data structures without mutexes.

For the latter topic, I have chosen to implement a concurrent stack and highlight some of the issues in designing such a data structure. Designing a mutex-free data structure in C++ that is platform independent is not a reality yet, so I chose GCC version 4.3.4 as the compiler and used GCC-specific __sync_* functions in the code. If you're a WIndows®C++ developer, consider the Interlocked* group of functions for similar work. Back to top Listing 1 shows the most basic concurrent, singly linked list interface. Listing 1. For the expected lines, Listing 2 shows the push_back method definition. Listing 2. Now, consider a thread trying to push n integers into this list in quick succession by calling push_back. Listing 3. Optimizing search elements Listing 4.

Listing 5. Listing 6. Listing 7. Multithreaded data structures for parallel computing. Introduction So, your computer now has four CPU cores; parallel computing is the latest buzzword, and you are keen to get into the game. But parallel computing is more than just using mutexes and condition variables in random functions and methods. One of the key tools that a C++ developer must have in his or her arsenal is the ability to design concurrent data structures. This article, the first in a two-part series, discusses the design of concurrent data structures in a multithreaded environment. For this article, you'll use the POSIX Threads library (also known as Pthreads; see Resources for a link), but implementations such as Boost Threads (see Resources for a link) can also be used.

This article assumes that you have a basic knowledge of fundamental data structures and some familiarity with the POSIX Threads library. You should have a basic understanding of thread creation, mutexes, and condition variables, as well. Back to top Designing a concurrent queue Listing 1. Listing 2. Avoiding memory leaks in POSIX thread programming. Introduction to POSIX threads The main reason to use threads is to boost program performance. Threads can be created and managed less operating system overhead and fewer system resources. All threads within a process share the same address space, which makes communication among threads more efficient and easier to implement than communication among processes.

For example, if one thread is waiting for an input/output system call to complete, the others can be working on CPU-intensive tasks. With threads, important tasks can be scheduled to take precedence over—and even interrupt—lower-priority tasks. And the main reason to use POSIX threads, or pthreads, is even simpler: As part of the standardized C language threads programming interface, they are highly portable. POSIX thread programming has many benefits, but if you're not clear about some basic rules, you run the risk of writing hard-to-debug code and creating memory leaks. Joinable threads Detached threads Back to top Recognizing leaks. POSIX Threads Programming. Table of Contents In shared memory multiprocessor architectures, threads can be used to implement parallelism. Historically, hardware vendors have implemented their own proprietary versions of threads, making portability a concern for software developers.

For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads. The tutorial begins with an introduction to concepts, motivations, and design considerations for using Pthreads. Level/Prerequisites: This tutorial is ideal for those who are new to parallel programming with pthreads.

What is a Thread? Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. What are Pthreads? Historically, hardware vendors have implemented their own proprietary versions of threads. Why Pthreads? Light Weight: Thread-safeness: Basic use of pthreads. Outside of Anne McCaffrey's writing in the Dragonriders of Pern series of novels, the word "thread" is most feared by programmers. Threads, sometimes called lightweight processes, are associated with gigantic, complicated projects. Library functions have dire warnings that they may not be "thread safe. " But what are these "threads? " What do you use them for? This article introduces threading with a simple threaded application.

What are threads? Threads are just like processes, only smaller. Threads often need to interact with each other, and in doing so, they run into the same problems that would be faced by multiple processes communicating using some kind of inter-process communication (IPC). Threaded programs also run into a few problems that are not normally seen with multiple processes and IPC. Back to top A simple program The sample program used for this article is a die-rolling program. The biggest distraction, in fact, is networking code. Play with this program a bit. Oops. AreaProg. POSIX Threads. Thread Basics: Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction. A thread does not maintain a list of created threads, nor does it know the thread that created it.

All threads within a process share the same address space. Threads in the same process share: Process instructions Most data open files (descriptors) signals and signal handlers current working directory User and group id Each thread has a unique: Thread ID set of registers, stack pointer stack for local variables, return addresses signal mask priority Return value: errno pthread functions return "0" if OK. Thread Creation and Termination: Example: pthread1.c Compile: Run: . Results: Thread 1 Thread 2 Thread 1 returns: 0 Thread 2 returns: 0 Details: In this example the same function is used in each thread.

Thread Synchronization: The threads library provides three synchronization mechanisms: Mutexes: Example threaded function: Practical threaded programming with Python. Introduction The Global Interpretor Lock refers to the fact that the Python interpreter is not thread safe. There is a global lock that the current thread holds to safely access Python objects. Because only one thread can aquire Python Objects/C API, the interpreter regularly releases and reacquires the lock every 100 bytecode of instructions. The frequency at which the interpreter checks for thread switching is controlled by the sys.setcheckinterval() function. In addition, the lock is released and reacquired around potentially blocking I/O operations. It is important to note that, because of the GIL, the CPU-bound applications won’t be helped by threads. With Python, there is no shortage of options for concurrency, the standard library includes support for threading, processes, and asynchronous I/O.

It is important to first define the differences between processes and threads. Hello Python threads Listing 1. hello_threads_example If you run this example, you get the following output: #!