Forum Archive

Permission error with multiprocessing.Process

max

I was testing the multiprocessing module and I ran this script on PC and it works fine, but using Pythonista on IOS gives PermissionError: [Errno 1] Operation not permitted. Anyone know how to fix this?

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob'))
    p.start()
    p.join()
JonB

multiprocessing is not supported on ios.

zrzka

It's b/o Apple, not b/o Pythonista. Only one process is allowed until Apple relaxes (if ever) this rule.

max

@zrzka thanks. Do you know of any alternatives that would be supported? I’ve tried threading but it doesn’t do exactly what I need

zrzka

And what exactly is what I need? No crystal ball here ;)

max

@zrzka sorry for being vague. I need to run a function multiple times in parallel pretty much

zrzka

What kind of function, I mean, what it does? I/O like networking, computational, ...?

Update: Can you elaborate more on what do you want to achieve?

max

@zrzka yeah. Basically it’s a program that fills and submits out a number of online forms using requests. And i’d Like to run it multiple times in parallel so it can fill multiple forms at a time.

JonB

Okay, so, Spambot :).

Though you cannot use multiprocessing, you can use asyncio /aiohttp and the like.

There are a few wrappers for requests
https://github.com/rdbhost/yieldfromRequests
Or
https://github.com/jsandovalc/aiorequests
Which allow use of asyncio and requests. I have not tried it on pythonista, but many examples of code that use aiohttp show huge increases in number of requests per second. Happy spamming...

max

@JonB thank you! I’m pretty new to python and coding in general so I appreciate your help

zrzka

I personally do use aiohttp, sanic, asyncio, aiomysql, ... and I really enjoy it. It's a huge difference in performance. Was doing some benchmarks for my Slack Applications / bots and the difference was noticeable, even by the end users. (Mostly prototypes / MVPs, reason for experimenting with sanic).

Anyway, here's an example. You can learn how to fire & wait for one request and how to fire & wait for many of them.

NOTE: No error handling & coindesk has rate limiting. Run this two, three times and then you have to wait.

import asyncio
import aiohttp


async def get_currencies(session):
    async with session.get('https://api.coindesk.com/v1/bpi/supported-currencies.json') as response:
        result = await response.json(content_type='text/html')
        return {x['currency']: x['country'] for x in result}


async def bitcoin_price(session, currency):
    async with session.get(f'https://api.coindesk.com/v1/bpi/currentprice/{currency}.json') as response:
        result = await response.json(content_type='application/javascript')
        return result['bpi'][currency]


async def bitcoin_prices():
    async with aiohttp.ClientSession() as session:
        # Get list of available currencies
        currencies = await get_currencies(session)

        # Prepare tasks for all currencies
        tasks = [
            asyncio.ensure_future(bitcoin_price(session, currency))
            for currency in currencies.keys()
        ]

        # Fire them and wait for results
        await asyncio.gather(*tasks)
        results = [x.result() for x in tasks]

        # Print them
        for x in results:
            print(' - {0} {1:12.1f} {2}'.format(x['code'], x['rate_float'], x['description']))


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


if __name__ == '__main__':
    main()