dgplug member blogs

Reader

Read the latest posts from dgplug member blogs.

from jason’s wotw blog

wotw, leisure

And to tell the truth I
don't want to let go of
the wrists of idleness, I
don't want to sell my life
for money, I don't even
want to come in out of
the rain.

And with this delightful Mary Oliver quote, this little blog, bids you adieu awhile.
I want to go and play in the rain :)

 
Read more...

from jason’s wotw blog

wotw, gut punch

means something knocks the wind out of you literally a punch to the gut. means to be suddenly be emotionally wound up, because of something you see or hear.

like I am about to do to you …


 
Read more...

from darshna

Here we will get to know about the If-else, control flow.

As the name says (if) it simply applies the condition applied to it. If the value of the expression is true, the command works according to it. #!usr/bin/env/ python 3 number= int(input("Enter a number:")) if number<100 print("the number is less than 100")

Else statement:– This is used when the (if)statement is not fulfilled.

#!usr/bin/env python3 number= int(input("Enter a number:") if number<100 print("The number is less than 100"); else print("The number is greater than 100");

 
Read more...

from mrinalraj

grep "unix" filename.txt : prints only line containing unix also higlights the “unix ” wherever it is present. unix is great os. unix is opensource. unix is free os. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

grep -n "unix" filename.txt : prints only line containing unix and also gives the no. line. 1:unix is great os. unix is opensource. unix is free os. 3:unix linux which one you choose. 4:unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

$grep "[2-3][0-9]" emp : Check for the string from 20 to 39 TCS101 Anu Engineer Manufacturing 30 35000 INFY02 Sona Developer Development 25 29000 TCS102 Raki Accountant Admin 31 32000 INFY03 Arun Engineer Manufacturing 28 34000 INFY04 Amit Accountant Admin 24 27000 TCS103 Nanda Engineer Manufacturing 29 37000 TCS104 Swathi Developer Development 28 29000

$ grep -v "[2-3][0-9]" emp :Invert the sense of matching, to select non-matching lines.

$ grep -n "[2-3][0-9]" emp : Displays in ordered list.(i.e. with line number) with string matching from 20 to 39. 1:TCS101 Anu Engineer Manufacturing 30 35000 2:INFY02 Sona Developer Development 25 29000 3:TCS102 Raki Accountant Admin 31 32000 4:INFY03 Arun Engineer Manufacturing 28 34000 5:INFY04 Amit Accountant Admin 24 27000 6:TCS103 Nanda Engineer Manufacturing 29 37000 7:TCS104 Swathi Developer Development 28 29000

$ grep -n "TCS[0-3][0-9][0-9]" emp : Displays only TCS employee details with line number present in the original file ranging from TCS000 to TCS399. 1:TCS101 Anu Engineer Manufacturing 30 35000 3:TCS102 Raki Accountant Admin 31 32000 6:TCS103 Nanda Engineer Manufacturing 29 37000 7:TCS104 Swathi Developer Development 28 29000

*NOTE : emp and student are the file names.*

 
Read more...

from mrinalraj

unix is great os. unix is opensource. unix is free os. learn operating system. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

sed -e 's/unix/UnIx/g' filename.txt : Searches for unix and replaces it with UnIx everywhere. Here g stands for making changes globally. UnIx is great os. UnIx is opensource. UnIx is free os. learn operating system. UnIx linux which one you choose. UnIx is easy to learn.UnIx is a multiuser os.Learn UnIx .UnIx is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed 's/unix/UnIx/1g' filename.txt : Replaces with Unix globally from the first occurrence in a line. UnIx is great os. UnIx is opensource. UnIx is free os. learn operating system. UnIx linux which one you choose. UnIx is easy to learn.UnIx is a multiuser os.Learn UnIx .UnIx is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '3,5s/unix/UnIx/' filename.txt : It will replace the unix with UnIx from 3rd line till 5th line if present. unix is great os. unix is opensource. unix is free os. learn operating system. UnIx linux which one you choose. UnIx is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '1,2s/unix/UnIx/' filename.txt: It will replace the unix with UnIx from 1st line and 2nd line. UnIx is great os. unix is opensource. unix is free os. learn operating system. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '1,$s/unix/UnIx/' filename.txt : It will replace the unix with UnIx from 1st line till the last line. Here “$” indicates end line. UnIx is great os. unix is opensource. unix is free os. learn operating system. UnIx linux which one you choose. UnIx is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '/unix/ a "Add a new line"' filename.txt : It adds “Add a new line” at the end of the line in which unix occurred. unix is great os. unix is opensource. unix is free os. “Add a new line” learn operating system. unix linux which one you choose. “Add a new line” unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. “Add a new line” linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '/unix/ i "Add a new line"' filename.txt : i means insert the “Add new line” before the occurence of unix “Add a new line” unix is great os. unix is opensource. unix is free os. learn operating system. “Add a new line” unix linux which one you choose. “Add a new line” unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

sed '/unix/ c "Add a new line"' filename.txt : replace the line containing unix with the content “Add new line” “Add a new line” learn operating system. “Add a new line” “Add a new line”

 
Read more...

from mrinalraj

In Python there is no need to import any external libraries for file handling as it comes with an inbuilt library There are 3 main operations associated with the file. 1. Creating/Opening a file. 2. Writing into the file/ Reading from the file. 3. Closing a file.


Step 1: Creating Opening a file :

Syntax : filePtr = open("your_file_name.txt", "mode") Following are the modes supported in python: 1. “w”, “r”, “a”: To open files in write mode, read mode and append mode respectively. ex: filePtr = open("my_file.txt", "w") It will create a new file if the given file name doesn't exist else it will overwrite the contents of the given file.

filePtr = open("my_file.txt", "r") It will open a file only if it exists in the reading mode. i.e. we cannot edit the contents of the file.

filePtr = open("my_file.txt" , "a") It will append the contents of the file if already exists else it will create a new one.

  1. “w+”, “r+”, “a+” : To open a file in write and read mode, read and write mode, and append and read mode.

filePtr = open("my_file.txt", "w+") It will create a new file if the given file name doesn't exist else it will overwrite the contents of the given file. In this mode, we can also read the content of the file at the same time.

filePtr = open("my_file.txt", "r+") It will open a file only if it exists in the reading mode also we can edit the contents of the file unlike in “r” mode. ** Note: +r differs from +w mode as in +r mode it doesn't delete the content also it doesn't create a new file if doesn't exist.**

filePtr = open("my_file.txt" , "a+") It will allow simultaneous read and append operation on the file.

  1. “wb”, “rb”, “ab”: To open files in write, read and append in binary mode respectively. ex: filePtr = open("my_file.txt", "wb") It will create a new file if the given file name doesn't exist else it will overwrite the contents of the given file.

filePtr = open("my_file.txt", "rb") It will open a file only if it exists in the reading mode. i.e. we cannot edit the contents of the file.

filePtr = open("my_file.txt" , "ab") It will append the contents of the file if already exists else it will create a new one.

  1. “wb+”, “rb+”, “ab+” : To open a file in write and read, **read and write **, and append and read in binary mode.

filePtr = open("my_file.txt", "wb+") It will create a new file if the given file name doesn't exist else it will overwrite the contents of the given file. In this mode, we can also read the content of the file at the same time.

filePtr = open("my_file.txt", "rb+") It will open a file only if it exists in the reading mode also we can edit the contents of the file unlike in “r” mode.

filePtr = open("my_file.txt" , "ab+") It will allow simultaneous read and append operation on the file.


Step 2: Writing in a file/ Reading from the file:

We can write in a file using write() function Syntax : filePtr.write("THIS is a file CONTENT")

We can read from the file using read() and readline() function ex: content = filePtr.read(): This return the entire content of the file. content = filePtr.readline(): This returns only the content of one line in a file.


Step 3: Closing a file

Closing a file is very important as if it exceeds the top limit it can lead to the crashing of the program. So it is advisable to close a file. Syntax : filePtr.close()

 
Read more...

from pradhvan

This is the second part of the series, in the first part we talked about the general idea of concurrency, how it's different from parallelism and saw how Python handles concurrency.

Part 1: Talking Concurrency -1

In the second part of the blog, we will look into the modern solution towards the problem using the new Asyncio module.

Import Asyncio

In the last post, we looked into a basic code snippet on how can we write concurrently. We also discussed some of the basic terminology used while using the Asyncio module. If you don't remember you should quickly take a recap as we would look at those concepts in a bit detailed manner.

Before looking at some code, let's understand some basic terminologies that would help in understanding the code better.

  • Eventloop: it's an infinite loop that keeps track of all the running tasks. It manages all the suspended functions and executes them when the time is right. These functions are stored in the queue called as the Task Queue, the event loop constantly polls the task queue and passes them to the event loop. When a task is passed on to the event loop it returns back a future object.

  • Future: a future is an indirect reference to a forthcoming result. It can loosely be translated as promise you make to do something when a condition is met, so when the condition is met a future can “callback” when ready to be executed. Since everything is an object in python, future is also an object that has the __await__() method implemented and its job is to hold a certain state and result. The state can be one of three things:

Pending: it does not have a result or exception yet. Cancelled: it was canceled Finished: it was finished either with a result or exception.

Futures also have a method called the add_done_callback() this is method allows the function to be called as soon as the task is completed with its process and is returned with a result. Which is the python object that would be returned with the expected result or raise an exception when the task is finished.

  • Tasks: a task executes a coroutine in an event loop. In a program, asyncio.create_task(coroutine) wraps the coroutine into a task and schedules its execution. asyncio.create_task(coroutine) returns a task object. Every time a coroutine is awaited for a future, the future is sent back to the task and binds itself to the future by calling the add_done_callback() on the future. From now on if the state of the future changes from either canceled or finished, while raising an exception or by passing the result as a python object. The task will be called and it will rise back up to its existence.

Since a typical program will have multiple tasks to be executed concurrently, we create normally with asyncio.create_task(coroutine) but we run them with asyncio.gather().

  • Coroutine: Asyncio was introduced in Python 3.4, initially it started off as decorator based coroutines @asyncio.coroutine which used a yield from keyword. Later in Python 3.5 async and await keywords were introduced which made working/reading concurrent code much easier. I won't go into much detailed on how coroutines evolved to the new async def keyword, because I planning to write a separate blog on that.

As we looked into the basic definition of coroutines in the last blog, we can loosely describe them as restartable functions.

You make a coroutine with the help of the async def keyword and you can suspend the coroutine with the await keyword. Every time when you await the function gets suspended while whatever you asked to wait on happens, and then when it's finished, the event loop will wake the function up again and resume it from the await call, passing any result out. Since coroutines evolved from generators and generators are iterators with __iter__() method, coroutines also have __await__() which allows them to continue every time await is called.

At each step a coroutine does three things:

  • It either awaits a future
  • It awaits another coroutine
  • It returns a result.

Before moving forward, I want to talk about await. In Python, anything that can be awaited i.e used with the await keyword is called an awaitable object. The most common awaitable that you would use would be coroutines, futures and tasks. Thus anything is blocking get's put to the event loop using the await and added to the list of paused coroutines.

Now let's look at a very basic async program to understand how everything fits in together.

import asyncio

import asyncio

async def compute(x, y):
    print("Compute %s + %s ..." % (x, y))
    await asyncio.sleep(1.0)
    return x + y

async def print_sum(x, y):
    result = await compute(x, y)
    print("%s + %s = %s" % (x, y, result))

asyncio.run(print_sum())

The sequence diagram below describes the flow of the above program.

tulip_coro.png

Now that we know all the basic terminology used in an async program let's look at a slightly complex code below for getting a better understanding all the jargons we learned above.

import asyncio


async def compute(x, y):
    """
    A coroutine that takes in two values and returns the sum.
    """
    print(f"Computing the value of {x} and {y}")
    await asyncio.sleep(1)
    return x + y


async def print_sum():
    """
    A coroutine that creates tasks.
    """
    value1 = asyncio.create_task(compute(1, 0))
    value2 = asyncio.create_task(compute(1, 0))
    value3 = asyncio.create_task(compute(1, 0))
    print(sum(await asyncio.gather(value1, value2, value3)))

asyncio.run(print_sum())

async def print_sum() and async def compute() are the two coroutines in the above program, the async def print_sum() as the main function used in the sync programming. The main function executes the entire program and all the functions related to it. The same approach is followed here, one coroutine awaits all the other coroutine.

Though this can be easily miss-understood, in that case, the program would just fine but would run in more like a sequential manner.

    value1 = await asyncio.create_task(compute(1, 0))
    value2 = await asyncio.create_task(compute(1, 0))
    value3 = await asyncio.create_task(compute(1, 0))
    print(sum(value1, value2, value3))

The above code can be a good example of how not to write async code, here using await on every task we are making all the calls sync thus making the program sequential. To avoid this asyncio.gather() is used in the program. To gather all the tasks in the program, value1, value2 and value3.

Finally, when all the tasks are gathered together, they are run concurrently.

Sync-Async-Sync

A lot of time you might be in a situation where you might have to call a sync function def from coroutine async def or have to call coroutine async def from sync function def. Ideally, you “shouldn't” use sync functions for calls that can be async like a database call because that is something that could provide further optimization. But there is nothing wrong with using a synchronous library for database, an async library for HTTP and gradually move things to async.

  • Sync-Async

Calling a sync function def from a coroutine async def. In that case, you run the sync function in a different thread using the threadpool executor. The runinexecutor() method of the event loop takes an executor instance, a regular callable to invoke, and any arguments to be passed to the callable. It returns a Future that can be used to wait for the function to finish its work and return something.

import asyncio
import concurrent.futures

def blocking_io():
    # File operations (such as logging) can block the
    # event loop: run them in a thread pool.
    with open('/dev/urandom', 'rb') as f:
        return f.read(100)

def cpu_bound():
    # CPU-bound operations will block the event loop:
    # in general it is preferable to run them in a
    # process pool.
    return sum(i * i for i in range(10 ** 7))

async def main():
    loop = asyncio.get_running_loop()

    ## Options:

    # 1. Run in the default loop's executor:
    result = await loop.run_in_executor(
        None, blocking_io)
    print('default thread pool', result)

    # 2. Run in a custom thread pool:
    with concurrent.futures.ThreadPoolExecutor() as pool:
        result = await loop.run_in_executor(
            pool, blocking_io)
        print('custom thread pool', result)

    # 3. Run in a custom process pool:
    with concurrent.futures.ProcessPoolExecutor() as pool:
        result = await loop.run_in_executor(
            pool, cpu_bound)
        print('custom process pool', result)

asyncio.run(main())
  • Sync-Async

When you have to call coroutines from the normal sync function. You just have to manually get_event_loo() , create tasks() and call the asyncio.gather() function. Since you can await, one thing you can do is create a queue with asyncio.queue() and use that queue to pass around the data between different coroutines.


import asyncio


async def compute(x, y, data):
    print(f"Computing the value of {x} and {y}")
    result = x + y
    await data.put(result)


async def process(n, data):
    processed, sumx = 0, 0
    while processed < n:
        item = await data.get()
        print(item)
        processed += 1
        value = item
        sumx += value
    print(f"The sum is:{sumx}")
    await asyncio.sleep(.5)


def main():
    loop = asyncio.get_event_loop()
    data = asyncio.Queue()
    sum1 = loop.create_task(compute(1, 4, data))
    sum2 = loop.create_task(compute(0, 0, data))
    sum3 = loop.create_task(process(2, data))
    final_task = asyncio.gather(sum1, sum2, sum3)
    loop.run_until_complete(final_task)


if __name__ == '__main__':
    main()

What now?

  • Just to get a better understanding of all the next syntax you learned, you can try out a sample problem mentioned below.

Write a program that reads log files and refires those URLs that have a 5xx status code. Once the refiring is done just add the &retry=True in the prefix of the URL and store them in a separate log file.

The log file will be a text file, you can check out a sample file here.

  • As I am still exploring the concept concurrency so I don't exactly know the best practices and pitfalls you should avoid while writing async code, but I highly recommend you check out asyncio: We Did It Wrong – roguelynn. This article can be a good followup after you are done with this one and are comfortable with syntax of asyncio.

Just before ending the blog I would like to thank maxking and Jason Braganza for helping me out in the blog.

In the next part of the series, I will be talking about threads and finally will conclude the series with asyncio based frameworks such as quart and aiohttp.

Happy Coding!

 
Read more...

from darshna

Be who you are, not who others tell you to be.

The above quote clearly replicates confidence and self-love. We somehow get affected by the negative comments and judgments passing by and try to change ourself so that people would accept us the way we are. Finally, when we arrange ourselves for the people to accept us there is again a judgment passing by and yet again we try to change ourselves. In the end, we forget the real us and be a toy for people's satisfaction. as we grow old we realize that we have left the true selves way back and been another person. Fat-shaming or Body shaming, being a dark-skinned girl or being extra bold even wearing a dark lipstick at daytime, or wearing clothes of your own choice attracts people's attention more than taking care of social and devastating issues happening day-to-day. Have you ever heard someone saying that look at the person who threw the packets on the side of a road or look at the person who is pissing at a public place, why aren't they ashamed? Yes, I am healthy and have a unique body shape that maybe most girls don't have or most guys make fun of it. So what? yes I am like this and I am proud of it but you will ignore it once or twice or maybe thrice. Ignorance is not a solution the mentality should be changed, and it can only change if people start teaching their children the value of respect and acceptance, acceptance of people as they are. That obviously doesn't include the acceptance of wrongdoings and violent nature of some people, but accepting the things to make this a society and a better place to live. Only then we can stand together and build a healthy community.

 
Read more...

from jason’s wotw blog

wotw, myth

the stories we tell ourselves as a culture, to explain what we could not really explain.

to help use make sense of the world, so that we’d not fuss over the unexplained and let us get on with our lives and make progress with the things we do understand.


 
Read more...

from darshna

After so many days I am continuing with Python again. Yes, I was not able to maintain the series properly, but let's get back to work and know what I read next in Python from the book of PYM. In Python most of the lines will have expressions and these expressions are made of operators and operands.

Operators

These are the symbols which tells the python interpretor to do the mathematical operation.

2+3
5
22.0/12
1.83333

To get floating results we need to use the division using any of operand as the floating number. To do modular operation use % operator.


#!/usr/bin/envv python3
days= int(input("Enter days:"))

month= days/30
days= days%30
print("Months= %d days= %d %(month, days))

Relational operators

operator ## meaning

< is less than > is greater than <= is less than or equal to >= is greater than or equal == is equal to != is not equal to

Note: // operator gives floor division result.

4.0//3
1.0

4.0/3
1.33333

Logical operator

To do a logical AND, OR we use these keywords.

 
Read more...

from mrinalraj

Today I tried to install UBUNTU 18.04 LTS into my friend's laptop. I will say it was quite a mixed experience. Below are the following steps I did for installing:

Step 1 : Partition

Click on windows icon type “Create and format Disc”. There you will see the following : Create and Format Disc

  • Right-click on C Drive.
  • Go to shrink Volume.
  • It is preferred to choose 30GB to 100Gb for UBUNTU OS. Here we will choose 60GB .i.e . 61440MB You will now see some free space being allocated. Note that during partition the unallocated free space must be at the end of all drives refer to this post on how to move unallocated spaces to the right.

Step 2: Creating a USB flash drive

Meanwhile, download the ISO file for UBUNTU 18.04 (LTS bionic). Also, download Etcher for creating flash USB. Give the path for downloaded iso file. Etcher image

Step 3 : Restart

Go to the BIOS setup. For my dell Vostro laptop. I will press F12 just as the dell image logo is visible. BIOS setup * Go to your USB drive * Select “Open UBUNTU without installing”

Click on the “Install UBUNTU 18.04.1 LTS” and it will ask for the following configuration.

  1. Select Normal Installation. Uncheck download updates while installing Ubuntu. Normal Installation Now there are two steps 1. Easy Step 2. Creating our configuration

    Easy Step

  2. Select “Install Ubuntu alongside Windows Boot Manager” and goto Step 8 Windows Boot Manager

    Our Configuration for home, root, and swap

  3. Select “Something ElseSometing Else Find where there is free space you have created. Free Space

  4. Select and click on the “+” icon and we will allocate the space for “swap” and “home” and “root” directory.

  5. Select logical memory and / signifies root, allocate 20 GB for it.

  6. Select swap memory for 4GB (half the RAM size is preferred, for my Laptop of 8GB, it's 4096 MB).

  7. Now give the rest of the space for the home.

  8. Now select install now. Install now and Continue Continue Click on continue. And wait for the installation to finish. Yay! we are all done.

 
Read more...

from darshna

I am reading the book Linux for you and me, and some of the commands I got to know and it's work!! Gnome Terminal Here in this terminal, we write the commands.

For example: [darshna@localhost~]

Here Darshna is the username, localhost is the hostname and this symbol `#~ is the directory name.

Following some commands are: * date command= tells us about current time and date in IST(Indian standard time) * cal command= displays the default present calendar. * whoami command= tells which user account you are using in this system. * id command=displays real user id. * pwd comma= helps to find the absolute path of the current directory. * cd command= this command helps you to change your current directory.

 
Read more...

from pradhvan

Whenever we think of programs or algorithms we think of steps that are supposed to be done one after the other to achieve a particular goal. Let's take a very simple example of a function that is supposed to greet a person:

def greeter(name):
    """Greeting function"""
    print(f"Hello {name}")

greeter(Guido) #1
greeter(Luciano) #2
greeter(Kushal) #3
"""
Output:
Hello Guido
Hello Luciano
Hello Kushal
"""

Here the function greeter() greets the person who's name is passed through it. But it does it sequentially i.e when greeter(Guido) will run the whole program will block it's state unless the function executes successfully or not. If it runs successfully then only the second and third function calls will be made.

This familiar style of programming is called sequential programming.

Why concurrency?

Sequential programming is comparatively easy to understand and most of the time fit the use case. But sometimes you need to get most out of your system for any X reason, the most common substituent of X, I could find is scaling your application.

Though greeter() is just a toy example but a real-world application with real user need to work the same even on huge amount of traffic it receives. Every time you get that spike in your traffic/daily active user you can't just add more hardware so one of the best solutions at times is to utilize your current system to the fullest. Thus Concurrency comes into the picture.

Concurrency is about dealing with lots of things at once. – Rob Pike

Challenges in writing concurrent programs

Before I move forward, I know what most of the people will say. If it's that important why at work/college/park/metro station/.. people are not talking about it? Why most of the people still use sequential programming patterns while coding?

Because of a very simple reason, it's not easy to wrap your head around and it's very easy to write sequential code pretending to be concurrent code.

concurrency-comic

I got to know about this programming style very late and later when I talked to people they said the same thing. It's not easy to code, you can easily skip the best practices and very hard to debug so most of the people try to stick to the normal style of programming.

How Python handles concurrency?

The two most popular ways(techniques) of dealing with concurrency in Python is through:

  1. Threading
  2. Asyncio

Threading: Python has a threading module that helps in writing multi-threaded code. You can spawn independent threads share common states (just like a common variable that is accessed by two independent threads).

Let's re-write that greeter() function again now with threads.

import threading 
import time
def main():
    thread1 = threading.Thread(target=greeter, args=('Guido',))
    thread2 = threading.Thread(target=greeter, args=('Luciano',))
    thread3 = threading.Thread(target=greeter, args=('Kushal',))
    thread1.start()
    thread2.start()
    thread3.start()

def greeter(name):
    print("Hello {}".format(name))
    time.sleep(1)
    
if __name__ == '__main__':
    main()

"""
Output:
Hello Guido
Hello Luciano
Hello Kushal
"""
    

Here thread1, thread2, thread3 are three independent threads that run alongside main thread of the interpreter. This may look it is running in parallel but it's not. Whenever the thread waits(here it's a simple function so you might see that), this wait can be anything reading from a socket, writing to a socket, reading from a Database. Its control is passed on to the other thread in the queue. In threading, this switching is done by the operating system(preemptive multitasking).

Though threads seem to be a good way to write multithreaded code it does have some problems too.

  • The switch between the threads during the waiting period is done by the operating system. The user does not have control over it.
  • Python has this lock called the GIL(Global Interpreter Lock) and the thread which holds the GIL can only run, others have to wait for its turn to get the GIL than only they can proceed. Which is great if you're doing an I/0 bound task but sucks if you're doing a CPU bound task.

Asyncio: Python introduced asyncio package in 3.4, which followed a different approach of doing concurrency. It brought up the concept of coroutines. A coroutine is a restartable function that can be awaited(paused) and restarted at any given point. Unlike threads, the user decides which coroutine should be executed next. Thus this became cooperative multitasking.

Asyncio brought new keywords like async and await. A coroutine is defined with the async keyword and is awaited so that the waiting time can be utilized by the other coroutine.

Let's rewrite the greeter() again but now using the Asyncio.

import asyncio


async def greeter(name):
	await asyncio.sleep(1)
	print(f'Hello {name}')


def main():
    loop = asyncio.get_event_loop()

    task1 = loop.create_task(greeter('Guido'))
    task2 = loop.create_task(greeter('Luciano'))
    task3 = loop.create_task(greeter('Kushal'))

    final_task = asyncio.gather(task1, task2, task3)
    loop.run_until_complete(final_task)


if __name__ == '__main__':
    main()

"""
Output:
Hello Guido
Hello Luciano
Hello Kushal
"""

Looking at the above code we see some of the not so common jargons thrown around, event loop, tasks, and a new sleep function. Let's understand them before we dissect the code and understand it's working.

  • Event loop: it's one of the most important parts of the async code, this is a simple code that keeps on looping and checks if anything has finished it's waiting and needs to be executed. Only a single task can be run in an event loop at a time.
  • Coroutines: here the greeter() is a coroutine which prints the greeting, though this is a simple example but in an I/0 bound process a coroutine needs to wait so await helps the program to wait and get off the event loop. The async.sleep() function is different from the time.sleep() because async.sleep() is a non blocking call i.e it does not hold the program until the execution is completed. The argument given to the async.sleep() is the at the most value of the wait.
  • Tasks: since a calling, a coroutine does not return the value of the coroutine it returns a coroutine object. Separate tasks are created that can function independently with the help of the coroutine.

Now let's move on to the code. Here task1,task2 and task3 work concurrently calling the coroutine. Once all the tasked are gathered the event loop runs until all the tasks are completed.

I hope this gives you a brief overview of Concurrency, we would be diving deep into both threading and asyncio and how can we use async for web applications using aiohttp and quart.

Stay tuned this will be a multi-part series.

While reading about concurrency you might a lot of other topics that you might confuse concurrency with so let's look at them now just so we know how is concurrency different.

Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once. Not the same, but related. One is about structure, one is about execution. Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable. -Rob Pike

  • Parallesim: doing tasks simultaneously, this is different from concurrency as in parallelism all the tasks run side by side without waiting(sleep) for other tasks, unlike a concurrent task. The method to achieve is called multiprocessing. Multiprocessing is well suited for CPU bound tasks as it distributes tasks over different cores of the CPU. Sadly Python's GIL doesn't do go well with CPU bound tasks.

  • Single-Threaded/Multi-Threaded: Python is a single-threaded language because of the Python's GIL but you can use multiple threads. These threads run along with the main thread. So threading, in general, is the method to achieve concurrency.

  • Asynchronous:, asynchrony is used to present the idea of either concurrent or parallel task and when we talk about asynchronous execution the tasks can correspond to different threads, processes or even servers.

Part 2: Talking Concurrency: Asyncio

 
Read more...

from darshna

#Did you know? We can assign multiple assignments in a single line.

For example:a,b = 10,20
a=10
b=20
You can use this method for swapping two numbers.
a,b=b,a
a=20
b=10

To understand how it works, we need to understand a datatype called tuple. We use a comma, to create the tuple; in the right-hand side we create the tuple(called tuple packing) and in the left-hand side we do tuple unpacking into a new tuple.

Let us take an example tp understnd more clearly.
data=("Darshna Das, West Bengal, Python")
name, state, language=data
name=Darshna Das
state=West Bengal
language=Python

Formatting a string Let us now see different methods to format string. .format method

name="Darshna"
language="Python"
msg="{0} loves {1}". .format(name,language)
print(msg)
Darshna loves Python.

Interesting fact In Python 3.6 a new way to do string formatting introduces a new concept called f-string.

name=Darshna
lanuguage=Python
msg=f"{name} loves {language}
print(msg)
Darshna loves Python.

f-strings provide a simple and readable way to embed Python expressions in a string.

 
Read more...