Book Image

Mastering PyCharm

By : Nafiul Islam
Book Image

Mastering PyCharm

By: Nafiul Islam

Overview of this book

Table of Contents (18 chapters)
Mastering PyCharm
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Dealing with threads and processes


PyCharm has very good support for dealing with threads. Just like how we can change frames, we can also change threads if we so wish (that is, if we have more than one thread to begin with). Let's take the example of a simple downloader script called downloader.py:

# encoding=utf-8
from threading import Thread

import requests


def download(url):
    response = requests.get(url)
    if response.status_code == 200:
        print "Success -> {:<75} | Length -> {}".format(response.url, len(response.content))
    else:
        print "Failure -> {:>75}".format(response.url)


if __name__ == '__main__':
    urls = "http://www.google.com http://www.bing.com http://www.yahoo.com http://news.ycombinator.com".split()

    for u in urls:
        Thread(target=download, args=(u,)).start()

Tip

To run this code, you'll need to install requests though pip install requests in the command line.

This is a simple script that sends a get request to a url (there...