An Introduction to Concurrency and Parallelism using Python Programming Language

Python concurrency concepts (like Multiprocessing, Multithreading, Coroutines, Asynchronous I/O ) will be explained in this tutorial. These are very relevant to production level code. If we can can apply these, then in many cases existing codes can be refactored and possibly we can make them simpler, improve response time and overall throughput (wherever applicable like a network server application). The concurrency models are equally important when implementing new applications.

An outline of the tutorial with approximate breakup of time is given below:

Process Based Parallelism (15 mins.)

  • Brief Overview of Process Management in Linux
  • Introduction to multiprocessing module
  • Parallelizing the execution of a function across multiple input values
  • (Data parallelism using pool of worker processes)
  • Communication between Processes - Pipes/Queues
  • Synchronization between processes
  • Sharing data between processes using Shared Memory

Coroutines (10 mins.)

  • Overview of Coroutines
  • Implementing Coroutine like functionality using "Extended Python Generators" (Python 2.5+)
  • Coroutines by leveraging sub generator delegation
  • Coroutines with async/await syntax

ProcessPoolExecutor, ThreadPool Executor (10 mins.)

  • ThreadPoolExecutor
  • ProcessPoolExecutor
  • Future Objects

Asynchronous I/O, event loop, coroutines and tasks (25 mins.)

  • Event Loop
  • Writing single-threaded concurrent code using coroutines
  • Multiplexing I/O access
  • Using Transport and Protocol classes for implementing Client/Server applications

Solving an assignment (30 mins.)

This session took place in track PyConDE and was classified suitable for some domain / basic python by the speaker.

Transcript (auto)

Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.

Speaker 1 [00:03]

friends good morning and thank you for coming okay so one small suggestion will you be able to simply download all the material which is available over here so since this is a tutorial talk I would expect that you can do a bit of coding or little bit of experimentation okay so and it's actually collectively we can learn it should not be a one-way type of stuff okay so some of you might be expert in this particular field okay so if i tell anything you would like to improve please feel free to do that okay so any problems i can just tell that what are the folders what folders will be available anybody able to download those stuff just can anybody show me what are the things you are getting yeah yeah so you can find a presentation that's the slide deck you can find a folder called assignment underscore web crawler one ms word document will be there inside that particular folder and there will be another folder named as source underscore codes okay so there there will be few dot py files I believe dot py files okay so just download those things first any query in downloading so three things should be available and if anybody wants pen drive that's also okay I do have a pen drive too I have executed the code using anaconda distribution okay so I can see that this is anaconda distribution but my python version is little bit dated it's 3.5 something so if any of you want to install anaconda in your windows machine right now I have the installer in my pen drive you can take it and install it it will take two three four minutes maybe but I can see many of you are having Mac so I don't have the Mac installer or you have Python already installed 3.5 plus version it's there already and if anybody wants that one that anaconda windows this one you can just let me know I have the installer in a pen drive anyone wants if anyone wants I can share it with you so the code will work in Python 3.5 at least okay so okay are we all settled down anyone if anybody wants you can just let me know that Windows installer good to go everybody downloaded okay okay so I'm waiting for another one two minutes okay so if we run out of time then i am bit lucky now that after this session there is which session there is a session yes normally lunch session is a good session to take please don't beat me okay feel free to do a bit of experimentation okay because the coverage in this particular case is a a bit wide coverage okay the basic principles are simple but a lot of syntactical stuff is also there that somehow is somehow sometimes difficult to remember actually but i will tell you the very very key points okay so within this 90 minute we won't be able to see all the apis which are available but the basic and key point of this topic okay like concurrency and parallelism it's a general topic and how to handle it in python and i believe that i will take this forward okay when you go back but even if we take a bit of time feel free to do the stuff over here any question is there please ask me i will try my best to answer and if anybody knows the stuff they can also pitch it okay now we are good to go can we shall we start shall we start you have a question oh okay oh okay okay yeah so everything so quotes and all that so now actually that means you don't need me i have given you everything so that i do basically before any session i give everything after that i can go for a lunch or breakfast okay okay thank you friends just let me open the presentation i may skip slides actually since it's a tutorial session you have everything you can little bit study of your own also okay so uh my name is this one okay so nothing is there in the name only a sequence of characters okay so if you transmit that you will basically lose some bytes okay and my gmail id is there at the end okay so i don't think we will be able to discuss everything today but you are always welcome to write me a technical or non-technical mail that means just saying a hello okay or anything so this one you if you want you can just I already you have okay everything fine this is that is a slight day clear from the back and you have it also you can open it yourself so this is basically the agenda okay means we'll start with few general questions okay i will ask you those questions and you will have to score some marks okay prerequisites after that we will see few definitions okay and after that process communicating between process synchronization between process sharing memory between processes then multi-processing launching parallel task example using pool generators co-routing async ios co-routine brief overview of socket programming then comparison then building a concurrent web crawler okay so let's not worry okay so it's basically a little bit of wide topics actually so i will tell you the key points okay so anybody is having any query on this topic or anybody who is aware of all the topic so that he can also aware or you have any specific question on any specific topic anybody who is uh means uh who wants to learn because i don't know actually not yet what exactly uh means so what are the levels you know okay so what shall i assume that we will shall i assume that we are having a lecture talk from ground up so as much as possible i try to teach you the stuff shall i assume that okay that will be easy for me okay fine so that means if that be the case okay so as usual as you know that my i am from india so my English will be a bit India like English okay so but still I am speaking slowly so can you understand me okay fine okay now this forget and forgive this particular stuff right now let's see the prerequisite that will be interesting and then why this prerequisite is there because there are a lot of topics I have shown okay and those topics will be mapped to some of these basic concepts okay these are common computer science concept and i would expect that everybody should be but we will be in sync with these points fine so let's start i'm not going to give any answer okay so this is an open this particular thing is an open forum what is the difference between a compiler and a interpreter volunteer anybody it can be right it can be wrong no problem okay we are trying to learn so please give an answer you have a mic also or no money if you speak everybody can hear so when you speak just little bit loudly that's so that everybody can hear and we can fight also we can fight technical fight okay technical fight means what let's say if in a technical thing somebody says that uh you have written a foolish code that doesn't mean that that person is telling me foolish he is telling that you have a lot of scope in improving the code so we can fight technically but not what physically yeah please that's okay have which one will be very fast okay so you are telling that see CPP since they are compiled type of means you compile them they will be very fast why they will be very fast very good so that means what he wanted to say is that that once you compile and create an executable and you can run that particular executable file multiple times and you don't have to compile it once more if not you change the source code okay but for an interpreter what happens so basically what he wanted to so which one so interpreter typically will be slower than a compiled stuff typically why because you have the overhead of invoking the interpreter when you run your source code okay in linux do you know how what's the typical means have you seen a executable file in linux just a general question in linux operating system or in mac also have you seen an executable file can you let me know the directory where the executable files are stored let me know the default directory yeah yeah very good yeah yeah yeah so can you just be little bit louder huh so can people can take a note and even take a note of these things okay uh thank you okay uh what's the difference between a process and a thread and if if this is clear then if we can answer this and what is context switching next i think yeah please very good so one point uh that as you told that process and thread the difference will be that it shares memory that is thread and a process won't be sharing memory okay so anything more a bit simpler process and thread what he told actually may not be technically always true for a particular operating system but what do you mean to say that one process id can be mapped to multiple thread ids okay okay so that's also but anything more simpler so what are the difference it means any advantages disadvantages of a process and a thread and can you give me an example of a process simply that will be forget about computer science say you have an apple mac machine or thinkpad or linux anything let me know a process let me know what show me a process show me how many processes are running in your system yeah exactly so if you have your mac machine command line terminal or linux terminal you can simply type what small ps and press and enter small ps press and enter and you get a list of all the processes there any number associated with the process any number how i can uniquely identify a process by using process id process identifier and see uh now what's the thread so you see that put it in simply without getting into a lot of technical details is that uh when i'm creating a process say this is one process and i am in linux like system or do anybody know how we create a process or if means anybody knows what's the call for creating a process anyone else has used the fork call fork call less number of people okay leave it so i have a process and from there i am creating another process so this particular process you say i am watching movie with this particular process okay another process has been created i am say listening to some audio so these are the processes but a lot of overhead is there in creating processes overhead means what i mean so why the term overhead is coming into the picture you will have to do a lot of memory copy operations for creating a process you will have to keep track of the stack heap these things are known stack heap of a particular process so there are there's some overheads associated computing overhead associated now if people thought that why but you will have to work with processes everything is basically a process so people thought that okay so a lot of overhead is there okay so let us think of something which is bit lightweight which is bit lightweight that means that the if i'm creating a thread which is also a control of execution like a process but i can have several threads in the context of a single process okay so imagine a process and there you have independent execution path and those are called thread so it is expected that creation of a thread will involve what less overhead okay the first important thing and that's so less overhead so and there are a lot of actually definitions available over internet so briefly speaking this that you have a big you have a process now for doing a particular task i could have created a separate process but now what i do i have a task t1 now i want to do a task t2 for this task t2 i could have created another process how there are functions operating system provides those apis to create a process but now i am not creating a process I want to reduce the overhead cost cost not in terms of euro but in terms of computing resources like time to data copy memory and all those things so now for doing the task t1 within process p I have created say t1 and t2 two thread so those threads are ideally linked as you told linked with a particular process they have something of their own they have something of their own what's the important thing they have of their own stack they have their own stack but the memory other memory area like data segment and all those things are basically shared so we expect to have some sort of optimization in creating a thread okay what is context switching very good so what is context switching have you seen context switching in your daily life have you seen context switching in your daily life see every time our manager actually makes us do a context switch in the morning i don't know about here but i have a in morning uh say 10 30 task t1 oh 11 30 task t2 so see task t1 to task t2 so when i have to switch then i will have to save the previous tasks pending stuff and then i go to task t2 and because i will have to come back again to task t1 so it's actually might be easy for computers and in computers have you seen context switching actually you have not seen but every time it happens why because in your computer several programs are running or processes are running at the same point of time but you haven't you have a feel but actually it's not the case a small time interval is there process p1 runs context switch happens for small time interval t2 process p2 runs context switch happens process p3 say in a round robin manner So, see, and who does all those things? Very good. The operating system scheduler. So, that means this context switching is actually doing no useful task. It's actually, again, an overhead. And the scheduler, operating system scheduler is coming into the picture which you don't know. this point to be noted please but point to be noted means please remember it's basically every time we are having context switching and normally for so many processes operating system scheduler comes into the picture and when operating scheduler operating system scheduler comes into the picture then overhead is there is this concept clear it's an important thing now what is the difference between cooperative and preemptive multitasking i think i should take another volunteer now yeah yeah that is what it is doing one so if i if i theoretically have 100 cpu and i have 100 processors okay so then i can actually schedule i don't i don't need them context switching imagining that io is also 100 io so i can simply schedule 100 tasks to 100 cpus but here the assumption is that obviously that it's you can have one cpu you can have multiple cores but your your processes are in thousands so it's mapping you can't have a practical mapping of the number of tasks or processes with the number of cpu or even the number of cores so in any case we can't avoid context switching and context switching is actually taking some time and it's actually wasting resources seriously it's wasting resources but we can't help we can't help yeah then anything is it clear obviously obviously it happens between threads also. Otherwise one thread will sit and won't release. So that means for process and thread, for both, context switching occurs. The technique might be little bit different but this must occur. Clear? And context switching is basically you are actually not gaining anything, your application program. That's an important thing. What's a cooperative and preemptive multitasking? Any other volunteer yes that is I think this particular take what you are suggesting this is a this is I think not sure this one is a bit optimistic okay so but in operating system we have this particular thing you are so as far as elements now but it's optimistic it's not exactly the thing but it is very very important concept and these first two three lines if it's clear then the entire python this stuff this stuff seems to be a bit complicated will actually be clear just tell me what is preemptive multitasking and what is cooperative multitasking and practically i mean say any operating system is it any making any sense the sense means that these are theoretical concepts or these are implemented in an operating system yeah a little bit louder preemptive means as you are telling the preempt means to stop or somehow to stop forcefully stop you continue then the next one stop you continue very good so you are coming to that here solve the problem so if that be the case that he is telling that i have several tasks or processes or thread and some scheduler is there and i have told you that there is a time slice okay so in this type of scenario or structure one process is running for say two millisecond then another process is running for two millisecond something something like that so is this the way of preemption or cooperation two millisecond but i need four but again he will now run so this is a preemption or a cooperation preemption means forcefully stop so it's a preemption so our operating system schedulers nowadays are what preemptive schedulers and not cooperative schedulers cooperative schedulers so you can give a you can just take a uh i mean okay you can check net afterwards also so our way schedulers are typically preemptive schedulers preemptive so processes threads they are preempted and cooperative multitasking is as such not there as far as i know it used to be in maybe windows 3.1 cooperative multitasking cooperative multitasking means what one process sits on the cpu and it will relinquish it only when it chooses to do that so that's the catch then why because you can actually starve all other which one yes yes yes yes cooperative one is not actually cooperative correct so it depends on the it depends on the individual why they were actually not cooperative is that that the funda is that when process p1 sits and process p2 also wants there is as such as such there was no communication between those two but if something could have been done that is that i have something something which is inside my process forget about thread now some entity inside my process and they can communicate also we don't know how they can communicate also and some library function or library stuff is there which controls the scheduling but not the operating system scheduler things can be a bit better or cooperative i want to tell what is this right now anyone can guess but guessing will end the story quickly okay so what i told you is that cooperative as he told cooperative didn't sound that much they are not cooperating actually okay so that means earlier days windows 3.1 so anybody can give us quick internet search and check that windows so three point you i'm telling correctly or not windows 3.1 multitasking or anybody is connected with internet so can anybody check just for a second that windows 3.1 multitasking cooperative or or preemptive windows 3.1 many of you might not have heard long back when i was in colleges 1994-95 okay windows 3.1 it's basically windows 3.1 multitasking is it cooperative or preemptive cooperative multitasking okay so that means that there was a possibility of processes getting hang because actually they couldn't cooperate okay but now imagine that this cooperation is a good concept i have something within the context of a process say something magical entities entity one entity two entity three etc and they are communicating okay and they are under the control of a scheduler of a scheduler but which is not the operating system scheduler so since i am not using the operating system scheduler i am actually spending less overhead and my task is also getting done so till now this sounds to be a good idea i believe okay but what is that i won't say right away am i clear where i am trying to head okay now now okay so but i think that uh then assume you have written program what is the difference between memory allocated between heap and stack you are aware mostly if not ever even when i bit complete i can tell you also anybody want to say the heap memory and stack memory very quickly okay anyone volunteer no answer then i leave it up to the leave it for you for homework not for homework just after i stop speaking then i can explain it to you once more no not sure c programming language anyone has used how many people have used c programming language very good so if so then you should be aware forgot and it's forgot malloc you remember a malloc malloc malloc is to malloc memory so malloc is getting memory from heap and what's the stack memory stack memory is basically the local variables are getting allocated from the stack okay so this is the crucial thing i think i need some more time to explain this but please wait let me continue and you just remind me once when i close and give you some assignment okay uh then to activation record anyone activation record what is an activation record no activation record means uh say when you are invoking a function invoking a function then some some stuff so um anybody so function f1 inside f1 i call f2 Inside F2, I call F3. So it's like F1, after that F2, after that F3. So corresponding to these calls, some part of the memory is getting allocated, which actually stores some stuff. So activation record is actually corresponds to a function call. So what can happen that you have a nested function call it's going up to say one lakh calls okay on the run time then finally what will happen i will run out of memory why because i will have to create the system will have to create nested calls are there okay or even you say recursive calls recursive calls okay so the recursion when you write a program it looks very good it's very nice very short and crisp program recursion. But when you run it, what happens? You give a big number and a lot of activation records are getting created. And finally, yeah correct, stack overflows. So that's actually the one of the very key point why this activation record is important. But you cannot estimate the, you can always estimate but you cannot actually tell that what will be the size of the activation record during compilation time I can tell the size of the activation record for a single function but how much it will grow because there can be in a recursive call yeah or it might depend on a user input also the how nested the functions are getting called? Clear? It should be there. It's there, some limit. But obviously, you should not try to hit that max tax size limit. But that is an important factor. So, what I mean to say is that people get a bit of a confusion that what exactly the thing, why exactly the things are getting overflowed. It's basically not a program bug or anything your program is running fine but there are a lot of activation records are getting created so it's basically running out of memory so there may not be any bug in your code sometimes it's basically the physical limitation or the virtual memory limitation actually that's that's the thing what is the system call system call means operating system call the operating some services okay yeah so keep few system calls example in line up system call means that some operating system calls which are used by everybody even your python interpreter is using your c language program is using so costly costly it operating system call okay so this little costly in terms of overhead so suppose f open f close these are c language file handling calls okay so in a higher level c language if you are using that inside the system calls are getting invoked okay any further question i will take okay what do you mean by an ipc give some examples assume you are browsing internet through a web browser is this an example of ipc inter-process communication some examples randomly system call means uh say operating system it basically gives your user program some services okay like f open f close these things are library functions and library function means the c library gives you that say or even you come to open functions in say python file handling function you are using python's open function but finally that python's open function will call will call the operating system service so there is a time also associated and a and a say time plus overhead why because from user level automatically it will go to operating system level and then return back but we are not aware of that so that's an important thing that in many cases you are calling a lot of system calls which but in most of the cases you won't be able to get rid of the system calls why because those are the basic building blocks of any even application program library okay so system call this is the system call you can give a bit of internet search and after the lecture i will again explain it to you once more it's clear the system very good pipe so can you give an example of a pipe in linux if i have to count the number of users who are logged in how i can use that huh very good who who pipe wc minus l interprocess communication we don't have a pain here okay it's a huh oh okay that's fine no that's okay because some of the huh well that's the thing I don't have any place to write it's not there okay Because computer science, computer science means it's not chalk and talk. Chalk and talk means that earlier days in the classroom, chalk means that white one. Have you seen? I have seen, but I think, okay, I don't, I think what you should have seen, I believe, chalk. huh but many people you have seen only the whiteboard and the nice pen markers and chalk the problem was that that if you're writing and then powder that dust powder little bit actually spreads okay but that was somehow manageable okay so inter-process communication any example let us take the example of browsing into browsing internet to a web browser is this an inter-process communication i want a clear explanation here it's important you want to say first or let us give him once the chance yeah please web browser you are explaining to okay very good and the process running the client or browser and then have these two processes communicate the HTTP message and that's why you need to do it. Excellent. Again. No, it's okay. Please. Can you please repeat? So, when we are opening the browser, that's basically a process. Okay. And that's a client process. and when we google something for example that's that's a server process and that's a google server running in a data center somewhere exactly and those two processes communicate with each other with messaging and in this case and that's why this is so it's basically an inter-process communication across a network anyone want to comment this one anything is not yeah you want to add no it's actually how what he told actually but we are bit generalized this particular thing inter-process communication it's not i won't take it hard and fast to be a in a single machine communication okay so let's generalize it but the network as you told as he mentioned that when you are communicating inside the same machine some examples common example in linux world you have already seen every day you you linux or your mac machine every day you use your pipe symbol pipe pipe means say suppose i want to count the number of users who are logged in or rather anybody uses the command line terminal every day how many people how many people everybody uses the command line terminal so you have the command line terminal can you uh can you count the number of uh directories which are there in your current directory i want only the count using some sort of communication technique count the count the number of directories which are there in your current directory you can physically count you can do an ls minus l 1 2 3 4 5 6 7 8 okay but i want a command which will give you at the end of the line that this this is the number say 30 i want the count of a number of directories in your current directory very quickly whoever answers correctly are done i little bit give them a what 10 second time we are counting huh the question clear i want the count of number of answer oh i think give people a fair chance okay fair scheduler a little bit time okay No, you have counted. I have counted with a comma. With a? With a comma. Okay, show me. Ls minus L pi Wc minus L is not correct. Ls minus L pi Wc minus L is not correct. There will be something else. I have told count only the number of directories. And don't, first thing first, first thing first, you need to figure out how to get the directories. In Linux, it's basically preceded by a D. I should be fine, right? Fine command you have used. But everybody may not be knowing the fact come on. ls minus l okay give that l or ls? ll is what? Oh you have made an alias oh great. it so then you can you are correct but there can be another way also to another couple of ways but people will understand i expected this so that you can a little bit explain it okay the thing now i give a bit how many people have done it thank you she has also done it let me check once ls minus no ls minus a means it also includes the hidden files okay so ls minus a wc minus l wrong wrong it is basically you might be having only directories keep some keep some regular files also touch file one touch file two you might be having only directory so you are getting the right answer. So it's not the first and foremost. I think let's, friend, can you just a little bit explain your answer? A little bit louder. Just explain the concept, okay, what you have actually done. You list everything inside your current folder by using ls longlisting minus l. then you pipe it into grep. Why? Because you need to filter out the directories. And how you can filter out the directories? By checking which line starts with a small d. What is the command for that? caret d. Double quote. Great caret d. And then I have got the filter of the directories, and now I simply have to count how many lines are there. That's basically the number of directories, pi WC minus L. Very good. If there can be other ways also, okay, by using find command and all, but just in giving an example of an IPC inside a single machine. Thank you. Okay. So these are the things. So that means what's the number of port number this one so as i assume browsing the internet that is also done as i distinguish between the terms now these are all networking related terms okay the quickly i am telling that ip means internet protocol icmp means internet control message protocol udp user datagram protocol http hypertext transfer protocol then everything is there so something some diagram is there you might have read it or checked it the tcp ip protocol stack okay anybody has used used means that aware of that particular diagram okay so these things are coming from there i'm not detailing these things right away okay so distinguish between a port number and a socket any idea port number and a socket or can you give that example once more from that network communication google to google server port number and socket no i need a simple definition of very simple definition of let me have this concrete example consider you are browsing google.com simultaneously through 10 different tabs slash windows let's not worry about trade process right now by using a browser like chrome from your computer in this scenario please suggest the number of ports that is port number needed locally that means in your computer from where you have invoked the browser and in the server consider there is a single server hosting google.com so that's basically i want the answer is the question clear How many ports on the client side? Very good, 10 ports on the client side. One port on the server side. is the port number of that server side in this particular case it should be port 80 since hypertext transfer protocol and so on a client-side port ten ports are we sure excellent so now the important thing here is that that one port number is actually well known that's the server port number in this particular case we have the server port number say 80 for http server okay now the server is also having an ip address ip1 assume or ips now there are 10 browsers i have opened from my computer so a tuple will be tuple tuple means record a fourth tuple you can consider fourth tuple how source ip address that means your computer's ip address source port number normally we don't if you have not done networking that port number people mostly consider that server port number but here we have a client port number also but normally a programmer don't have to assign a client port number normally why because whenever the networking starts we will see those are automatically assigned so if i consider a record record is having four components source ip source port number destination ip and destination port number so if i have four tuple 10 times and these tuples are unique these tuples are unique what are tuple tuple means a record what type of record i am considering record means source ip source port destination ip and destination port so for all these 10 record three things are common what are the three things correct destination server port destination uh ip address and source ip address only one thing is changing that's the source port number source port number so the data based on the source port number gets demultiplexed into 10 different browser windows so this explains that why data of one browser window is not getting mixed up with the data of another browser window is this clear clear no confusion clear clear too many new words but the concept is well explained okay no new word but i made it simple actually okay so port number is not hardware it's just a number okay what how i can check port number what is the command by which i can check the port numbers uh in say how many what are the port numbers opened in linux or uh what you are using mac os what's the command typically used there's a command typically used yeah excellent net stack you can simply run that command in your that black window net stat very useful command network statistics n e t s t a t you can use grape and all those things to figure out many things i think you use that command is a very important command net stat small okay oh race condition critical section mutual exclusion simple manner in a simple manner okay race condition critical section and mutual exclusion mu take semaphore condition variable anybody wants a simpler anybody can explain these things in a simple manner quickly correct mutual exclusion Okay, fair enough. A little bit for everybody, yes. They share some kind of memory where the producer writes something and the consumer needs to read that. Exactly. Then we get various conditions because it's not allowed to have this memory section in both. Or two persons need to write at the same time. Correct. That's not allowed. for the mutix and the simulco I might guess that simulco is like a token that is owned and when it stands it gives away huh it's funny and mutix is kind of a lock a lock on mutix right now I'm consuming this resource please do not do anything with it when I'm finished I reveal the lock okay now let me summarize it quickly okay so see if i have a i have a writing pad i have a writing pad so consider this a memory okay now i now tell all of you okay all of us are processes or threads right here start writing i am writing he is writing he is writing so what what's this called now what will be the final output of this diary what will the i'm trying to explain one thing so i'm a diary okay i'm inviting you come write something i am also writing what will be the final output the final output is not known the final output is not known why because one small diary and everybody has come invited to write in it without any control so basically the output cannot be predicted okay so this is an example of what race condition where you cannot predict the output because everybody is trying to do something at the same point of time but if i would have told you to come and read read something from the diary whatever has been written then you could have read it that won't do as such any damage he can when everybody can just can be a cue a cue or if you are having a binocular you may simply read it okay so race condition is that you imagine uh processes or threads that try trying to write something or access something okay which is shared and the output is not known that's the example of a race condition you have the race condition also non-determinism you have the race condition also in where digital logic i remember college days what flip-flop to avoid race condition finally we made i've forgotten what jk master slip flip-flop or something like that you have read flip-flop when the red means hard flip-flop there was some race condition typical questions used to come that how i can convert the simple laser flip-flop to or something more so that to avoid the race condition okay so that is also a race condition and this is the software race condition okay clear now mutex semaphore condition variable so that's he and moreover he actually told a nice term it's basically a mutual exclusion mutual exclusion means what that access to this particular area which is basically so uh this particular area should be mutually exclusion mutually exclusive of one another when i am accessing him writing i cannot allow him to write I cannot allow you to write when you are writing I cannot allow him to write but it's not inherently taken care by the operating system inherently these things are not taken care by the operating system so uh say consider a banking transaction you are writing something you are withdrawing money at the same point of time if somebody else in somewhere also tries to do something transaction okay then what will happen the result will not exactly be known or it will be a garbage result okay so these are the examples of race condition that means you should ensure that race condition what doesn't occur so how to do this particular thing there are ways there are ways okay one of the way is that that and moreover the problem is that operating system is not giving you an operating system cannot guarantee in your application code that whether these conditions are avoided or not that's the difficult thing of programming okay now these are some of the and what is this critical section critical section is basically can be defined as that part of the code which tries to access that shared resource it's not exactly the my diary so she i'm considering him to be a piece of say code and his eyes or hands are accessing that so that part of the code which particularly accesses that area is called critical section or critical region take a note i think you're taking a note and please give an internet search also okay afterwards okay i will ask questions we'll take a test after this okay so critical section these mutex semaphore condition variable okay so see all those things are it's again there are a lot of things are written on the internet but the bottom line thing is that these are some primitives these are some primitives primitive means what operating system api which will help you to avoid these conditions these are some primitives like say as i told you that what i can do now is that now i am having that in my diary again taking this diary example writing everybody including me process or thread okay and my body this part hand say this is a piece of code okay what I will do and imagine that there is a lock there is a lock global lock now what I will do is that everybody's body before he writes before he uses the hand lock criticals before entering the critical section use a code called lock and after coming out write a code called unlock and that lock can be accessed by everybody so once he his code does that lock if everybody of us tries to get that lock we won't be able to get it because that's a global lock and he can write something so he executes his critical section then he comes out of it and before coming out he has unlocked and now other people can take turn if he now gets the lock write something on my diary then coming out unlock clear that's the basic thing so these so so again mutex is a bit simpler as you told lock unlock semaphore similar thing can be means somebody calls somebody calls a mutex to be a you have heard of it interview question binary semaphore lock unlock two state lock other people tries to do unlock can't until and unless a particular thread unlocks that semaphore somewhat more features than a mutex okay this was the first primitive If it was conceptualized by a famous computer scientist named as, you can, should know the name, Dijkstra. E.W., very good. Who told Dijkstra? Oh, great. E.W. Dijkstra. Can you spell it from internet? E.W. Dijkstra, Dijkstra's algorithm. him so he so should be even if you don't listen my talk at least if you can learn about dixtra that will also make sense okay so mutex semaphore unconditioned variable a bit sophisticated why a bit sophisticated is basically what that sometimes right that uh here as you can understand that there is a bit of slipping slip slipping means locking then doing something then unlocking okay so that means some sort of busy waiting is always there for other processes busy waiting means what it's basically trying to see whether the lock is there or not all of you are there you have done a lock you have done a then you are writing in my diary at that point of time all the other people trying to check trying to poll that's actually waste CPU cycles okay that is called busy waiting so to avoid that we can have a bit of sophisticated primitive called condition variable okay basic thing this one i am leaving it right now may not be required today but i will come back after some time leave that re-entrant code anybody is aware of what is a re-entrant code okay tell yeah but it's a bit it will require a bit of more explanation actually it will be your i understand that you have but i'm just leaving it for now okay so static analysis dynamic analysis any idea re-entrant code means if i don't talk of thread it won't be able to explain it anybody static analysis dynamic analysis as such not required for this class static analysis of a program and dynamic static analysis you know because you have used what you have used gcc so tell me the name of a static analyzer used for ccpp oh any open source thing that i also don't know static analyzer can you tell anybody okay very good for python it will be pylint pylint this one uh how many okay so if i have this piece of code okay and then if i simply run static analysis i am getting so much number so i am good 9.47 out of 10 why i may have fixed my code okay fixed means to show you i may have made the static analyzer happy actually but somehow or the other Now, if I simply, if I simply say, comment out, okay, and now I have commented out the function doc stream. Now, I am running the, okay. it is basically piling basically checks the code not it's a statically analyzing the code why because there is a guideline called pep8 pep8 python enhancement proposal 8 where you have a coding guideline so critically important for production level code and even if you are in academics please try to follow any academic professors are there from anybody is there from academics here academics or academics means i mean to say uh studying now or teaching in a college anyone you are very good okay so here if you can get a chance tell people to use for any languages the static analyzers actually so it's basically good coding practice clear take a note of that pi lint p-y-l-i-n-t n-t lint this is called linting pi p-y-l-i-n-t pi lint l-i-n-t okay now see see what we will what we will do actually is that as usual that we are having now the basic background now what i will do is that i will tell you so this particular slide is having all the code and all those things fine i will tell you the flow because if i have to browse through each and every slide that i won't be able to do it okay it will take time because already scheduled time another 25 minutes left even if i drag it i won't be able to cover all the slides so so don't worry what i will do i will tell you the what is the flow and then the important slide and the variation and still we are here you can just go through the slide once okay now let's come back to the so this you now have you now actually know most of the thing okay the only thing python so please remember process trade context switching overhead inter-process communication race race condition uh then another important thing we told uh we told what cooperative preemptive those things everything now you know now what is to be shown is just let's follow this one and we will understand see now now see true para now one a few okay all we have seen the definitions and just check a few more definitions actually. This is an important slide. Read it for a minute. Read it for a minute, this slide. which one is not clear? This is critical. So, we are trying to now, can you tell me that why, first of all, the word parallelism and concurrency they are not the same the first thing first okay pure parallelism is actually running two processes or two thread simultaneously see and then ideally you are having multiple processors here. This is an example of parallelism. Parallely. But if you have a single CPU and we know it very well, that is time shared. So, and if I consider the scheduling to be preemptive scheduling and that is the case. So, for a single CPU, single core CPU, I can't have pure parallelism. So what I get that's basically something virtual. The time slices are so small that we are not, we cannot see the context switch. We understand that only one program is running at the same point of time. I have 10 browser window, but you have a single CPU. Will you be able to run parallelly 10 browser window? No. You cannot. But they are running you can say they are running concurrently and that's an example of simple example no time sharing so that's the difference between a parallelism and concurrency trade process we know this one now is this thing clear if your operating system is operating system running preemptively means the operating system scheduler is running the applications applications preemptively exactly but then i cannot write any parallel application or i can you can't write any parallel application cannot okay but this is okay cannot so in a single theoretically cannot in a single you have a single processor i can two cpu say two process means say parallel means that is shared one thing is shared you have a single core process p1 is scheduled to this core and process P2 is scheduled to this core, okay. So, at that point, you can tell that these two are running parallelly. Parallelly. But the word, somebody takes this parallelism and concurrency in a similar manner. But the thing is that the pure parallelism will come. Continuously this is running and continuously this is running. And in a single computer, a single CPU, you can basically run how many at a time? One. One, single code. So, basically you are achieving pseudo-parallelism, something like that, concurrency. Parallel means so many parallel control unit and parallel. So, that's a big costly thing. So, we will have to boil down finally to something which is easier, basically. Now, what is this one, coroutine? Coroutine. Now, the question is that why we are trying to understand this parallelism and concurrency? Why, first of all? does it huh that's the catch the first answer will be speed up but is it that only speed up that's my question is it that only speed up no at least uh uh okay means speed up but actually the technically what will be the term exactly exactly or in other word i can say that i can optimize the waiting times i can optimize the waiting times what happens see if i were running a networking process and it's trying to face some data and at that point of time but it will have to wait to get the data I cannot make it fast it will have to wait but at that point of time I can actually make him away from that blocking IO and can schedule somebody else but in any case that time what that person will take he will take to get the data so using using concurrent method I cannot I cannot make that time shorter of IO time shorter. Clear na? In any case, we are fetching network data. Whatever time is required, that too you will take because this depends on the network. So what this construct will help us to do? At that point of time, when I am waiting to get some data, at that time the other people are getting blocked. That is bad. So I will put him on some side and try to schedule other people at that point of time so as he told so it will effectively optimize the waiting times clear so it's not only speed up clear clear okay so coroutine this is our definition now we will see what is that but remember process thread everything is preemptive and coroutines are non-preemptive or cooperative fine now uh now for this python how the things will go now it's having it's having process it's having thread so you can write a multiple process code okay typically where this are multi multi-threaded code typically when do you need to write this type of code for most of the professional applications okay you need to write a multi-threaded code or multiple processes code why because in many of the application that's a client server applications are there okay so several requests are coming from the client and your server your server needs to handle those requests so your server cannot make all the people wait arbitrarily that means this ensuring concurrency is actually crucial that anybody should not be getting blocked that's the important thing okay so this is important in our life any professional piece of networking code or some other code these constructs will be used and another thing important thing it's not only for the speed up it actually what important thing is that to optimize the waiting times why because even if uh even if because for an i o you might have seen for an i o even today in my laptop for when i am accessing a disk it's too slow even if i have a say what this uh what intel 8 or what is this intel processor intel 5 6 intel what is i5 sorry i5 i5 i7 processor but still io it's slow so i will have to spend that time even to make i mean it means how i will save the time i cannot save and even in the operating system i have seen it's not properly even this windows operating system is not properly handled that io part sometimes it get hangs so i will have to give that time but during that time i will have to ensure that other people are not, other processes are not getting blocked. That's one of the key point, okay? So the techniques are what? You can use multiple processes. Multiple processes, you can use multiple threads, everything supported by Python, okay? Comparing threads and process, at least in Python, which one is preferable, more preferable? If you have Yeah, definitely. very good so if you are using a process but if i have multiple processors at least i can ensure that i have now two three processors say now multiple i have multiple processes so the processes can be scheduled to multiple processors so now i get some time benefit because i have multiple processors but even in this case python threads implementation won't allow won't allow you to schedule it in multiple processors okay so threads are a bit preferable than processes okay so I believe that we can take a bit of more time okay no problem now little or you are just you want to leave at one I think in 15 minutes more will be okay okay so now key point I will tell and share the presentation with you we will hear for another at least 15 20 many 15 minutes I believe that please don't please please don't get angry okay so we'll have a lot of lunch fine okay good thank you and so process now quickly i am telling you what are the important things to take a note from this slide then you will be just i will tell you a little bit of things to do obviously not complete complete things okay so see this is a simple example of using a process okay so follow this toc table of content we won't be confused okay follow this table of content process-based parallelism so communicating between processes synchronization between processes why it is required required to avoid the race condition okay sharing memory between processes it is basically one form of ipc called shared memory multi-processing using process pool now see basically this one is not but this one and the first one only difference is that process pool gives you a little bit of more sophistication some more abstraction but whatever you can do using process pool you can do using the simple process also okay so these are the because python has also evolved version and version okay so initially there might be this one after that this one after that this one i think that's the logic using process pool now threading module you can use the threading module but to more simpler using the thread pool but it does mostly the same thing okay now Now, this coroutine, I will, this is an important thing, async IO, recently been introduced, recently been, I think, Python 3.3 or 4 plus, that's basically is a bit more sophistication, which I told you the cooperative multitasking. Co-routines are some entities inside, say, one process or a thread which can communicate with each other, does a cooperative multitasking, and under the purview of the library. Operating system is not getting involved. So, highly scalable. Highly scalable. Well, so, okay, now comparison and building a concurrent web server will be your assignment, homework. Okay, tomorrow we will check. See, is the flow clear? So I'm not going, so I won't be able to talk of a lot of code, but is the basic concept clear or any question here? Okay, now let us see a bit of code, like, say, so this is the process code. very simple but the same thing can be done using a process pool also which is a sophistication of this process api but basically they are the same linux process using fork system call okay communicating between processes using pipe but normally we don't need all those things because you have the pipe in your shell is communicating between processes using pipe communicating between processes using queue okay all codes are there only thing is that that as i think we are running a short of time you do it yourself i am here but i will actually end up at around 110 or 115 whoever wants to go for lunch they can go but i am here anybody can ask me any question but let me at least complete the key talk that is fine communicating synchronization i have already told you about the race condition example of using a lock simple race condition i have skipped the semaphore part sharing memory between processes it's basically called shared memory shared memory okay process pool process pool is a little bit of a easy to use api compared to the process process pool pool means say in this particular piece of code uh everything is there with you okay so in this particular piece of code i have used a process pool i have brought a process pool and what the code is supposed to do i want to find out all the files and directories inside my root directory okay now i get the path i get those path path means how many directories are there within my current directory and throw those path to the pool of processes so this function using this function i am getting the list of directories in my current directory and i want to walk through those so every absolute path i am actually giving to that process pool by using the map function so map means what one process getting mapped to one argument okay this is example of a process pool using threading module see threading module is basically how you can create a thread so i am creating several thread to get few images so creation of a thread threading dot thread thread dot start and i am having a list here to get those thread ids fine and trying to count and this please remember that normally you will be joining the thread joining means you wait for the threads to complete fine fine so thread create thread then do some activity inside the thread what is the activity here download image So, you can simply run the code and see. Download image user function. So, this function will be invoked by every thread. And how many threads are getting created? That is image URLs. There is actually some URLs. Okay. You can put some URL. So, if you put 10 URLs, 10 threads will be created. So, now, in parallel, again, the question in parallel doesn't mean that threads are running in parallel because I have one CPU. Okay. So, it means that context switching happens, but when one thread is getting, say, doing that IOTAS, another thread gets scheduled, another thread gets scheduled, so it's not actual parallelism, and in any case, as he explained, in Python, the thread, the catch is that the interpreter doesn't allow the threads to get scheduled to multiple cores due to an architecture, okay, that's basically, it is having a global lock, that's the but in any way threads can be used. Okay, threading module, similar thing with the help of thread pool. What I can do with threading module, I can do with thread pool, but thread pool gives you a little bit more advantage. Higher level API, nothing big issue, okay. Similar thing using process pool, same thing can be done using process also, but this pool they are giving you a bit of more flexibility. Given some assignment, now this initial part is generator based coroutine was earlier used. Now, so we have now understood what is the coroutine. What is the coroutine? Coroutine process trade everything is preemptive. That means operating system comes into the picture okay so overheads are there context switches happen now we have something called coroutine so imagine coroutine to be some entity inside one thread or a process and now they can actually work cooperatively under the control of a scheduler but that scheduler is not the operating system scheduler that scheduler is the library scheduler so should be fast clear but it looks to be little bit tricky but it's simply it seems that i have coroutines so this is basically bit important you have i think several might i don't know here normally in conferences you have lectures on this async io itself actually so async io is a module name i am having three coroutine okay three coroutine how you define a coroutine it's basically that is actually sorry these are generator based coroutine earlier syntax so let me let me start with this one so this one should be used the earlier syntax can may be skipped actually so now async dev coroutine one async dev coroutine two so these are three coroutines basically what are these something like a thread but however they are not preemptive they are not impacted by preemptive scheduling they will run of their own inside the context of a thread and they will be controlled by a thread scheduler not the thread scheduler but a but a asynchio scheduler okay so here you see these are the three coroutines and near here here this await await means what relinquish control relinquish control await that means I am now gone you can take control see it's not a normal function call it's little bit it's the imagination is that this see this statement this particular statement say you are getting three tasks here this particular statement basically three coroutines are there it simply floats the three coroutine inside that thread now one coroutine two coroutine three coroutine okay they are not getting controlled by the operating system scheduler but they are like thread imagine one big thread three coroutines okay this await means relinquishes control okay but it doesn't mean that slipping two second means this slips two second that slips two seconds and this slips one second that is basically five second no that's not the thing when he is slipping for two second at that time control is released another person also can slip another person could also they are not getting blocked. But the only thing is that these calls, please take a note of that, these calls are not the standard library calls. This sleep is the async IO sleep. That is the only catch. One thread, you cannot see a thread. Even in one thread, now you have created three coroutines and simply have floated them here floated them now and the loop is running until completed now they can run basically concurrently and it's basically the cooperative multitasking happens and they are controlled by the library here not the OS so that means overhead is less clear right clear means at least remember after that I am here you can ask questions so this is basically as far as so I am not telling anything best good or worst or something like that I am telling you the technique so I have actually discussed all the three techniques what are the t techniques if it is having process it is having thread it's having now async io okay apart from this async io there are other third-party packages also like say greenlet twisted those are the things but that not in python standard library tries to do something similar okay and what and what if i now okay this is a networking piece of code okay let's let's not worry about the networking stuff leave it for the time being let me just tell you the differences and that time you can have a now let's see this theoretical part and this will make the things more clear whatever the things we are not able to do what i believe that you will be doing just after lunch i am here as i told you that the code part this is the thing important three things are there that's basically python concurrency and parallelism okay i have tried to give in the overview every topic can be can become a individual two-hour lecture of its own so see this particular thing another important thing is that in my slide everywhere whatever or possible i have given the references that is important here this particular slide this particular thing some references taken from this youtube link he is i think a good programmer and something even i was thinking that you can actually debate you can actually debate it's that nothing that whatever i have written in the bow or whatever has been uh copy pasted or edited is actually to be true so we can debate and fight so are you agreeing mostly agreeing with that something is written by me so after thinking so are you everybody mostly agreeing so that means again i am not telling through this slide which one is good which one is bad which one is better i'm just comparison you have thread you have processes you have coroutines asynchio coroutine so it's up to you okay so i'm not biasing that here the thing is that even in my mind that overall say scalability so here directly he says that he says particularly that in case of a process the scalability is low but C programmers will tell that oh what you are telling I can write a process code using Linux system calls only and now the system calls are very optimized so I can scale up the code so why do I need to use async then this type of arguments can be there okay but in general it makes sense scalability low means that so lot of lot of processes overhead so less scalable coroutine as you can understand that as you can understand what inside the context of a thread so no preemptive switching no thread switching that means should be very lightweight bottom what's the minus point minus point is that socket calls normal socket calls or blocking calls you won't be able to use the sleep call has been used but that sleep call is from asyncio sleep call okay then thank you so no argument little bit no fighting okay thank you then so i am here for

Tanmoy Bandyopadhyay

About — in the speaker's own words

I am having around 19 years of work experience in academics and industry (Aricent, Infosys). I am a post graduate in Information Technology.

Social card for talk: An Introduction to Concurrency and Parallelism using Python Programming Language